gpt4 book ai didi

python - 事先不知道字段时使用 DictWriter 编写 CSV

转载 作者:太空宇宙 更新时间:2023-11-04 10:30:49 24 4
gpt4 key购买 nike

我正在将一大段文本解析到字典中,最终目标是创建一个 CSV 文件,其中的键作为列标题。

csv.DictWriter(csvfile, fieldnames, restval='', extrasaction='raise', dialect='excel', *args, **kwds)

问题出现了,因为任何第 n 行的字典都可以包含一个新的、以前从未使用过的键。然后我希望 CSV 也包含这个新 key 的列。简而言之,我所有的字段都是事先不知道的,所以我无法在开始时编译一个完整的 fieldnames

有没有推荐的方法来获得 csv.DictWriter不忽略缺失的字段,而是将它们添加到 fieldnames 中?此时仅更改 fieldnames 会使前面几行的字段数量错误地减少。

最佳答案

我尝试使用 csvwriterow 方法,而不是使用 DictWriter 这在您的情况下可能会造成混淆,因为字典没有排序。这是我所做的:

"""
a) First took all the keys of dictionary and sorted it, which is not necessary.
b) Created a result list which appends value related the headers which is key of our input dict and if key is not available then .get() will return None.
So result list will contain lists for rows data.
c) Wrote header and each row from result list in csv file
"""

data_dict = [{ "Header_1":"data_1", "Header_2":"data_2", "Header_3":"data_3"},
{ "Header_1":"data_4", "Header_2":"data_5", "Header_3":"data_6"},
{ "Header_1":"data_7", "Header_2":"data_8", "Header_3":"data_9", "Header_4":"data_10"},
{ "Header_1":"data_11", "Header_3":"data_12"},
{ "Header_1":"data_13", "Header_2":"data_14", "Header_3":"data_15"}]

"""
In the third dict we have extra key, value.
In forth we dont have have header_2 were we aspect blank value in our csv file.
"""
process_data = [ [k,v] for _dict in data_dict for k,v in _dict.iteritems() ]

headers = [ i[0] for i in process_data ]
headers = sorted(list(set(headers)))

result = []
for _dict in data_dict:
row = []
for header in headers:
row.append(_dict.get(header, None))
result.append(row)


import csv
with open('demo.csv', 'wb') as csvfile:
spamwriter = csv.writer(csvfile, delimiter=';', dialect='excel',
quotechar='|', quoting=csv.QUOTE_MINIMAL)
spamwriter.writerow(headers)
for r in result:
spamwriter.writerow(r)

enter image description here

关于python - 事先不知道字段时使用 DictWriter 编写 CSV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26771999/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com