0

I am trying to take sensor data from online api to a csv file using csv library. Whenever I am trying that i am getting multiple values appended in a single row that is increasing so on...

['22.00'] ['71.00'] ['0.00']
['22.00', '22.00'] ['71.00', '71.00'] ['0.00', '0.00']
['22.00', '22.00','22.00'] ['71.00', '71.00','71.00'] ['0.00','0.00', '0.00']

this is the output i get. Here is my code:

while True:
    data_temp = requests.get(temp_url).json()
    data_hum = requests.get(hum_url).json()
    data_flame = requests.get(flame_url).json()
    feilds_temp = data_temp['feeds']
    feilds_hum = data_hum['feeds']
    feilds_flame = data_flame['feeds']
    with open('testdata.csv','a') as csv_file:
        for x in feilds_temp:
            temp.append(x['field1'])
        for y in feilds_hum:
            humid.append(y['field2'])
        for z in feilds_flame:
            flame.append(z['field3'])
        csv_write = csv.DictWriter(csv_file, fieldnames= fieldnames)
        data = {
            "temperature": temp,
            "humidity": humid,
            "flame": flame
        }
        csv_write.writerow(data)
        print(temp, humid, flame)
    time.sleep(15)
3
  • try replacing "csv.writerow(data)" with "csv.writerows(data)" Commented Jan 25, 2020 at 9:38
  • If that doesn't work, try csv_write.writerow([data]) - that particular CSV writer lies things in lists Commented Jan 25, 2020 at 9:42
  • Nope, that doesnt work either. I still get the same format Commented Jan 25, 2020 at 9:48

1 Answer 1

2

You're not clearing the lists each time through the loop, so you're appending to the previous readings.

Use list comprehensions to create the lists fresh each time, instead of appending.

while True:
    data_temp = requests.get(temp_url).json()
    data_hum = requests.get(hum_url).json()
    data_flame = requests.get(flame_url).json()
    feilds_temp = data_temp['feeds']
    feilds_hum = data_hum['feeds']
    feilds_flame = data_flame['feeds']
    with open('testdata.csv','a') as csv_file:
        temp = [x['field1'] for x in feilds_temp]
        hum = [x['field2'] for x in feilds_hum]
        flame = [x['field3'] for x in feilds_flame]
        csv_write = csv.DictWriter(csv_file, fieldnames= fieldnames)
        data = {
            "temperature": temp,
            "humidity": humid,
            "flame": flame
        }
        csv_write.writerow(data)
        print(temp, humid, flame)
    time.sleep(15)
Sign up to request clarification or add additional context in comments.

3 Comments

Is there any way to convert that data into integer?
I'm not sure what you mean. There's no datatypes in CSV files.
Do you mean get rid of the .00 in the numbers? int(float(x['field1'])). float() will parse the number, and int() will then discard the fraction.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.