简体   繁体   中英

Continuously write data from a list into a CSV file

I'm trying to get my python script to keep writing to a .CSV file and I'm quite the beginner when it comes to python.

I've tried to follow this person which has a similar problem but without success: Writing List of Strings to Excel CSV File in Python

I'm currently trying to save a list of sensor data containing temperature, humidity and a time, the code currently look like this:

def writeDataToFile():
    sensorData = []
    sensorData = getSensorData()


    with open("/home/pi/Documents/Python Projects/CSV-files/sensor_data.csv", 'w') as csv_file:
        writer = csv.writer(csv_file, delimiter=',')
        writer.writerows(sensorData)

The error i'm getting is this: interable expected, not int So the first data values is stored in a int i assume?

I'm used to java where you define types but here you just type the variable so uhm yeah.

I've tried writer.writerow(sensorData) and it works then, but as you might already expect, it just writes a single row of data.

The rest of the code is running in a while true loop where it keeps storing data into the list.

How do i get this function to keep writing to my csv file, adding more and more as my loop keeps running?

The getSensorData function:

def getSensorData():
    sensorData = []

    temperature = getTemperatureFromSensor()
    humidity = getHumidityFromSensor()
    time = getCurrentFormatedTimestamp()

    sensorData.append(temperature)
    sensorData.append(humidity)
    sensorData.append(time)

    return sensorData

So i tried to simply print out the list and it does exactly what i want it do to:

[29, 21, '2017-10-30 15:02:47']

[29, 21, '2017-10-30 15:02:52']

[29, 22, '2017-10-30 15:02:57']

[29, 21, '2017-10-30 15:03:02']

[28, 21, '2017-10-30 15:03:07']

[28, 21, '2017-10-30 15:03:13']

etc, that's basically what i want to store into the csv

The writerows method of the csv module expects a nested list where each inner list represents the data for a single row. In your case, passing a flat list, it instead finds an int value at the 0th index where it's instead expecting the inner list for the first row. That explains the error message.

So, you should instead be using writerow each time you call your method. However, opening the file in write mode ( 'w' ) will delete all the contents of the file each time. Therefore, you need to open the file in append mode. A toy example:

import csv

def write_csv(data):
    with open('example.csv', 'a') as outfile:
        writer = csv.writer(outfile)
        writer.writerow(data)

for x in range(5):
    write_csv([x, x+1])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM