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