将多个Python字典写入csv文件 [英] Writing multiple Python dictionaries to csv file

查看:1229
本文介绍了将多个Python字典写入csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢这个其他线程,我已经成功地使用Python作为初学者将我的字典写入csv:
Python:将字典写入csv文件,每行一个key:value

  dict1 = {0:24.7548,1:34.2422,2:19.3290} 



csv如下所示:

  0 24.7548 
1 34.2422
2 19.3290

现在,我想知道最好的方法用相同的键组织几个字典。我想把键作为第一列,然后在列后面的dict值,所有第一行,通过字典名称来区分列。



当然,有很多线程试图做类似的事情,如:使用键作为标题和值作为列将字典写入csv,但是没有以相同的方式构建数据...)。也许词典必须首先合并。

  dict2 = {0:13.422,1:9.2308,2:20.132} 
dict3 = {0:32.2422, 1:23.342,2:32.424}

我的理想输出:


b $ b

  ID dict1 dict2 dict3 
0 24.7548 13.422 32.2422
1 34.2422 9.2308 23.342
2 19.3290 20.132 32.424

我不知道,但是,键名称的列名称ID如何工作。任何帮助将不胜感激。 Mahal提前!

解决方案

使用csv模块和列表推导:

  import csv 

dict1 = {0:33.422,1:39.2308,2:30.132}
dict2 = {0:42.2422,1:43.342 ,2:42.424}
dict3 = {0:13.422,1:9.2308,2:20.132}
dict4 = {0:32.2422,1:23.342,2:32.424}

dicts = dict1,dict2,dict3,dict4

with open('my_data.csv','wb')as ofile:
writer = csv.writer(ofile,delimiter ='\\ \\ t')
writer.writerow(['ID','dict1','dict2','dict3','dict4'])
用于dict1.iterkeys )writer.writerow([key] + [d [key] for d in dicts])

默认情况下,字典是无序的,所以如果你想要按升序排序,你必须排序键:

  in sort(dict1.iterkeys(),key = lambda x:int(x)):
writer.writerow([key] + [d [key] for d in dicts])

如果您需要处理无法确定所有词典具有相同键的情况,您需要更改小东西:

 以open('my_data.csv','wb')asile:
writer = csv .writer(ofile,delimiter ='\t')
writer.writerow(['ID','dict1','dict2','dict3','dict4'])
keys = set (d.keys()for d in dicts)
for键in:
writer.writerow(d)(d.get(key,None)for d in dicts])


Thanks to this other thread, I've successfully written my dictionary to a csv as a beginner using Python: Python: Writing a dictionary to a csv file with one line for every 'key: value'

dict1 = {0 : 24.7548, 1: 34.2422, 2: 19.3290}

csv looks like this:

0  24.7548
1  34.2422
2  19.3290

Now, i'm wondering what would be the best approach to organize several dictionaries with the same keys. I'm looking to have the keys as a first column, then the dict values in columns after that, all with a first row to distinguish the columns by dictionary names.

Sure, there are a lot of threads trying to do similar things, such as: Trouble writing a dictionary to csv with keys as headers and values as columns , but don't have my data structured in the same way (yet…). Maybe the dictionaries must be merged first.

dict2 = {0 : 13.422, 1 : 9.2308, 2 : 20.132}
dict3 = {0 : 32.2422, 1 : 23.342, 2 : 32.424}

My ideal output:

  ID  dict1    dict2    dict3
  0   24.7548  13.422   32.2422
  1   34.2422  9.2308   23.342
  2   19.3290  20.132   32.424

I'm not sure, yet, how the column name 'ID' for key names will work it's way in there. Any help would be appreciated. Mahalo in advance!

解决方案

Use the csv module and list comprehension:

import csv

dict1 = {0: 33.422, 1: 39.2308, 2: 30.132}
dict2 = {0: 42.2422, 1: 43.342, 2: 42.424}
dict3 = {0: 13.422, 1: 9.2308, 2: 20.132}
dict4 = {0: 32.2422, 1: 23.342, 2: 32.424}

dicts = dict1, dict2, dict3, dict4

with open('my_data.csv', 'wb') as ofile:
    writer = csv.writer(ofile, delimiter='\t')
    writer.writerow(['ID', 'dict1', 'dict2', 'dict3', 'dict4'])
    for key in dict1.iterkeys():
        writer.writerow([key] + [d[key] for d in dicts])

Note that dictionaries is unordered by default, so if you want the keys in ascending order, you have to sort the keys:

for key in sorted(dict1.iterkeys(), key=lambda x: int(x)):
    writer.writerow([key] + [d[key] for d in dicts])

If you need to handle situations where you can't be sure that all dicts have the same keys, you'll need to change some small stuff:

with open('my_data.csv', 'wb') as ofile:
    writer = csv.writer(ofile, delimiter='\t')
    writer.writerow(['ID', 'dict1', 'dict2', 'dict3', 'dict4'])
    keys = set(d.keys() for d in dicts)
    for key in keys:
        writer.writerow([key] + [d.get(key, None) for d in dicts])

这篇关于将多个Python字典写入csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆