简体   繁体   English

将列表中的数据连续写入CSV文件

[英]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. 我正在尝试让我的python脚本继续写入.CSV文件,而且当涉及到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 我试图跟随这个有类似问题但没有成功的人: 在Python中将字符串列表写入Excel CSV文件

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? 我得到的错误是:interable expected,not int所以第一个数据值存储在int我假设?

I'm used to java where you define types but here you just type the variable so uhm yeah. 我已经习惯了你定义类型的java,但在这里你只需输入变量就可以了。

I've tried writer.writerow(sensorData) and it works then, but as you might already expect, it just writes a single row of data. 我已经尝试过writer.writerow(sensorData)然后就可以了,但正如你可能已经预料的那样,它只是写了一行数据。

The rest of the code is running in a while true loop where it keeps storing data into the list. 其余代码在while循环中运行,它将数据存储到列表中。

How do i get this function to keep writing to my csv file, adding more and more as my loop keeps running? 我如何让这个函数继续写入我的csv文件,随着我的循环继续运行而添加越来越多?

The getSensorData function: getSensorData函数:

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:47']

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

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

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

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

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

etc, that's basically what i want to store into the csv 等,这基本上是我想要存储到csv

The writerows method of the csv module expects a nested list where each inner list represents the data for a single row. csv模块的writerows方法需要一个嵌套列表,其中每个内部列表表示单个行的数据。 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. 在你的情况下,传递一个平面列表,它会在第0个索引处找到一个int值,而不是在第一行的内部列表中。 That explains the error message. 这解释了错误消息。

So, you should instead be using writerow each time you call your method. 因此,每次调用方法时都应该使用writerow However, opening the file in write mode ( 'w' ) will delete all the contents of the file each time. 但是,以写入模式( 'w' )打开文件将每次删除文件的所有内容。 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])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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