无法将列表中的值写入 csv python

Unable to write values from list to csv python

我正在尝试将我的 InfluxDB 数据库中的一些数据写入 csv。虽然 influxdb-cli 确实允许我将数据导出到 csv,但我不允许自己 运行 服务器上的 cli。我正在使用 influx-db python 包来查询数据库。

现在,这是我正在使用的代码:

import csv
from influxdb import InfluxDBClient

USER='root'
PASSWORD='root'
DBNAME='sensordata'
HOST='localhost'
PORT=8086

client = InfluxDBClient(HOST,PORT,USER,PASSWORD,DBNAME)
query="select * from home where time > '2017-11-15 14:55:00' AND time <= '2017-11-15 14:55:05' tz('Asia/Kolkata')"
result = client.query(query,epoch='ns')
exported_data = list(result.get_points())
with open("output.csv", "a", newline='') as fp:
    writer = csv.writer(fp, dialect='excel')
    for line in exported_data:
        print(line)
        writer.writerow(line)

问题是当我打印这些行时,我得到了键和值,如下所示:

{'time': 1510737900336919297, 'value1': 18.84, 'value10': 19.83, 'value2': 18.56, 'value3': 12.61, 'value4': 17.57, 'value5': 16.6, 'value6': 16.81, 'value7': 12.84, 'value8': 11.54, 'value9': 14.26}
{'time': 1510737901370333995, 'value1': 11.32, 'value10': 12.98, 'value2': 12.34, 'value3': 12.22, 'value4': 11.08, 'value5': 12.07, 'value6': 17.62, 'value7': 14.68, 'value8': 16.87, 'value9': 11.4}
{'time': 1510737902403461281, 'value1': 12.37, 'value10': 16.18, 'value2': 18.83, 'value3': 14.59, 'value4': 11.79, 'value5': 18.52, 'value6': 11.25, 'value7': 17.28, 'value8': 10.54, 'value9': 19.1}
{'time': 1510737903436997966, 'value1': 13, 'value10': 12.04, 'value2': 10.02, 'value3': 14.28, 'value4': 14.51, 'value5': 17.3, 'value6': 16.14, 'value7': 15.04, 'value8': 13.16, 'value9': 10.47}
{'time': 1510737904470366806, 'value1': 16.2, 'value10': 10.83, 'value2': 12.64, 'value3': 13.51, 'value4': 13.74, 'value5': 11.52, 'value6': 13.42, 'value7': 13.14, 'value8': 16.6, 'value9': 11.24}

但是,csv 文件仅包含如下密钥:

time,value1,value10,value2,value3,value4,value5,value6,value7,value8,value9
time,value1,value10,value2,value3,value4,value5,value6,value7,value8,value9
time,value1,value10,value2,value3,value4,value5,value6,value7,value8,value9
time,value1,value10,value2,value3,value4,value5,value6,value7,value8,value9
time,value1,value10,value2,value3,value4,value5,value6,value7,value8,value9

我做错了什么?

此外,我是否可以将 csv 格式设置为:

time_heading, value1_heading, value2_heading ....
time_value, value1_value, value2_value ....
time_value, value1_value, value2_value ....
time_value, value1_value, value2_value ....
.
.
.

我确实尝试了一些我在谷歌上搜索过的解决方案,但其中 none 有帮助。有谁知道出了什么问题?

编辑

我还想 "natural_sort" 加入 headers,因为采用那种格式很有意义。为此,我参考了 this answer on SO. But, to do that, I needed to ignore the first item which is "time" and sort the "value" headers only which I did using

我需要做的最后一件事是计算文本的偏移量,因为一些测量值的字段名称为 "val",而有些测量值的字段名称为 "value"。偏移量分别为 3 和 5,我将在自然排序代码段中使用它们。为此,我参考了 this。我终于让数据看起来像

time,value1,value2,value3,....value10 而不是 time,value1,value10,value2,value3,...value9,这正是我要找的。

最终代码如下所示:

import csv
from influxdb import InfluxDBClient

USER = 'root'
PASSWORD = 'root'
DB_NAME = 'sensordata'
HOST = 'localhost'
PORT = 8086

client = InfluxDBClient(HOST, PORT, USER, PASSWORD, DB_NAME)
query = "select * from home where time > '2017-11-15 14:55:00' AND time <= '2017-11-15 14:55:05' tz('Asia/Kolkata')"
result = client.query(query, epoch='ns')
exported_data = list(result.get_points())
header_list = list(exported_data[0].keys())

with open("output.csv", "w", newline='') as fp:
    writer = csv.writer(fp, dialect='excel')
    # print(header_list[1:])
    value_header = header_list[1]
    offset = sum(c.isalpha() for c in value_header)
    # print(offset)
    header_list[1:] = sorted(header_list[1:], key=lambda x: int(x[offset:]))
    # print(header_list)
    writer.writerow(header_list)
    for line in exported_data:
        # print(line)
        writer.writerow([line[kn] for kn in header_list])

感谢@be_good_do_good的回答

因为你不仅有值的迭代,还有 key:value 对的字典,你应该使用 csv.DictWriter 而不是 csv.writer

您需要按照以下方式进行操作。这种方式存在csv中写入数据顺序随机的问题:

with open("output.csv", "a", newline='') as fp:
    writer = csv.writer(fp, dialect='excel')
    writer.writerow(exported_data[0].keys())
    for line in exported_data:
        print(line)
        writer.writerow(line.values())

你做的一切都正确,但是当你传递字典时,键被视为list,所以你需要指定需要写入csv的内容 - line.values()

如果您对键的顺序感兴趣,请按以下方式进行:

with open("output.csv", "a", newline='') as fp:
    writer = csv.writer(fp, dialect='excel')
    header_list = ['time,value1,value10,value2,value3,value4,value5,value6,value7,value8,value9']
    writer.writerow(header_list)
    for line in exported_data:
        print(line)
        writer.writerow([line[kn] for kn in header_list])

对字典使用 csv.DictWriter(),对列表使用 csv.writer()。

with open('output.csv','a') as file:
    writer = csv.DictWriter(file, exported_data[0].keys())
    writer.writeheader()
    writer.writerows(exported_data)