将多个字典写入CSV,并使用Python的一个标题 [英] Writing multiple dictionaries to CSV with one header with Python

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

问题描述

我试图写多个字典到csv文件,其中header(key)只写一次,行(值)基于键写。我能够找到两个字典,但如果我得到多个字典需要写的情况如何?



我是流媒体转换为json的tweets,所以我试图结束一个CSV文件,按每个JSON键排序。这里是我想做的更详细的解释(在Python中将多个JSON写入CSV - 将字典转换为CSV
这是我要结束的,但是有成千上万个潜在的数据行(如果可能,最好按键排序):



这是我的两个字典的基本代码:

  import csv 

my_dict = {'key1':'value1','key2':'value2','key3':'value3','key4':'value4','key5':'value5','key6':'value6' key7':'value7'}
my_dict2 = {'key1':'value1A','key2':'value2A','key3':'value3A','key4':'value4A','key5': 'value5A','key6':'value6A','key7':'value7A'}

open('mycsvfile.csv','wb')as f:
w = csv .dictWriter(f,my_dict.keys())
w.writeheader()
w.writerow(my_dict)
如果my_dict.keys()== my_dict2.keys $ b w.writerow(my_dict2)

print my_dict




我是一个挖掘者!

解决方案

然后,迭代该列表,并只写入键匹配的行。像这样:

  keys = ['key1','key2','key3','key4','key5' ]#等等。

dictionaries = [my_dict,my_dict2]#只是一个例子,你可能在流式传输tweets时构建此列表。

with open('mycsvfile.csv','wb')as f:
w = csv.DictWriter(f,my_dict.keys())
w.writeheader
for d in [x for x in dictionaries if x.keys()== keys]:#only matching keys。
w.writerow(d)


I am trying to write multiple dictionaries to a csv file, where header (key) gets written only once and rows (values) are written based on the key. I was able to figure this out for two dictionaries, but what if I am getting multiple dictionaries that need to be written?

I am streaming tweets which get converted to json, so I am trying to end with a CSV file that is sorted by each JSON key. Here is a more detailed explanation of what I am trying to do (Writing multiple JSON to CSV in Python - Dictionary to CSV Here is what I am trying to end up with but with thousands of potential rows of data (preferably sorted by key if possible):

Here is my basic code for two dictionaries:

import csv

my_dict = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3', 'key4': 'value4', 'key5': 'value5', 'key6': 'value6', 'key7': 'value7'}
my_dict2 = {'key1': 'value1A', 'key2': 'value2A', 'key3': 'value3A', 'key4': 'value4A', 'key5': 'value5A', 'key6': 'value6A', 'key7': 'value7A'}

with open('mycsvfile.csv', 'wb') as f:
    w = csv.DictWriter(f, my_dict.keys())
    w.writeheader()
    w.writerow(my_dict)
    if my_dict.keys() == my_dict2.keys():
        w.writerow(my_dict2)

print my_dict

P.S. I am a begginer!

解决方案

You can put the multiple dicts in a list. You then iterate over that list and only write the rows where the keys match. Something like this:

keys = ['key1', 'key2', 'key3', 'key4', 'key5'] # and so forth..

dictionaries = [my_dict, my_dict2] # Just an example, you probably build this list while you stream the tweets.

with open('mycsvfile.csv', 'wb') as f:
    w = csv.DictWriter(f, my_dict.keys())
    w.writeheader()
    for d in [x for x in dictionaries if x.keys() == keys]: # only matching keys.
       w.writerow(d)

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

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