字典到.csv文件

时间:2017-10-12 20:21:09

标签: python csv dictionary

似乎无法解决这个问题。 我有一个字典,看起来像这样:

sorted_metrics = {
    "1-Aug-2017": {"Messaged":1,"Rejected":2}
    "2-Aug-2017": {"Messaged":3,"Rejected":2}
    "3-Aug-2017": {"Messaged":4,"Rejected":2}
    "4-Aug-2017": {"Messaged":2,"Rejected":2}
    "5-Aug-2017": {"Messaged":3,"Rejected":2}
    "6-Aug-2017": {"Messaged":1,"Rejected":2}
    "7-Aug-2017": {"Messaged":5,"Rejected":2}
    "8-Aug-2017": {"Messaged":0,"Rejected":2}
    "9-Aug-2017": {"Messaged":4,"Rejected":2}
}

我希望我的.csv文件看起来像这样:

Date         Messaged    Rejected
1-Aug-2017   1           2
2-Aug-2017   3           2
3-Aug-2017   4           2
4-Aug-2017   2           2
5-Aug-2017   3           2
6-Aug-2017   1           2
7-Aug-2017   5           2
8-Aug-2017   0           2
9-Aug-2017   4           2

csv.writerow要么将每个字符放在一列中,要么我不能将这些信息连接成一行。

4 个答案:

答案 0 :(得分:2)

总的来说,这就是你需要的:

l = [[k, v['Messaged'], v['Rejected']] for k,v in sorted_metrics.items()]
with open("output.csv", "wb") as f:
        writer = csv.writer(f)
        writer.writerow(['Date','Messaged','Rejected'])
        writer.writerows(l)

详细说明:

1-您可以将字典转换为列表列表

In [31]: sorted_metrics
Out[31]: 
{'1-Aug-2017': {'Messaged': 1, 'Rejected': 2},
 '2-Aug-2017': {'Messaged': 3, 'Rejected': 2},
 '3-Aug-2017': {'Messaged': 4, 'Rejected': 2},
 '4-Aug-2017': {'Messaged': 2, 'Rejected': 2},
 '5-Aug-2017': {'Messaged': 3, 'Rejected': 2},
 '6-Aug-2017': {'Messaged': 1, 'Rejected': 2},
 '7-Aug-2017': {'Messaged': 5, 'Rejected': 2},
 '8-Aug-2017': {'Messaged': 0, 'Rejected': 2},
 '9-Aug-2017': {'Messaged': 4, 'Rejected': 2}}

In [32]: 

In [32]: l = [[k, v['Messaged'], v['Rejected']] for k,v in sorted_metrics.items()]

In [33]: l
Out[33]: 
[['9-Aug-2017', 4, 2],
 ['2-Aug-2017', 3, 2],
 ['6-Aug-2017', 1, 2],
 ['3-Aug-2017', 4, 2],
 ['4-Aug-2017', 2, 2],
 ['1-Aug-2017', 1, 2],
 ['7-Aug-2017', 5, 2],
 ['8-Aug-2017', 0, 2],
 ['5-Aug-2017', 3, 2]]

2-然后按如下方式写入csv:

with open("output.csv", "wb") as f:
        writer = csv.writer(f)
        writer.writerow(['Date','Messaged','Rejected'])
        writer.writerows(l)

输出:

Date,Messaged,Rejected
9-Aug-2017,4,2
2-Aug-2017,3,2
6-Aug-2017,1,2
3-Aug-2017,4,2
4-Aug-2017,2,2
1-Aug-2017,1,2
7-Aug-2017,5,2
8-Aug-2017,0,2
5-Aug-2017,3,2

答案 1 :(得分:1)

如果您愿意使用pandas

df = pd.DataFrame(sorted_metrics).T
df.index.name = 'Date'
df.to_csv('bla.csv', sep=' ')

答案 2 :(得分:0)

for key1, value1 in sorted_metrics.iteritems(): for key2, value2 in value1.iteritems(): #do something

这会遍历字典,让您可以访问所有数据,我可以让您正确处理这些数据。

答案 3 :(得分:0)

Promise.all(
  recordInformationList.map(recordInfo => 
    OtherDescription.destroy({
      where: { id: recordInfo.OtherID }
    })
  )
)
.then(values => values.every(n => n > 0))
.then(bool => bool);

随意更改import csv sorted_metrics = {'1-Aug-2017': {'Messaged': 1, 'Rejected': 2}, '2-Aug-2017': {'Messaged': 3, 'Rejected': 2}, '3-Aug-2017': {'Messaged': 4, 'Rejected': 2}, '4-Aug-2017': {'Messaged': 2, 'Rejected': 2}, '5-Aug-2017': {'Messaged': 3, 'Rejected': 2}, '6-Aug-2017': {'Messaged': 1, 'Rejected': 2}, '7-Aug-2017': {'Messaged': 5, 'Rejected': 2}, '8-Aug-2017': {'Messaged': 0, 'Rejected': 2}, '9-Aug-2017': {'Messaged': 4, 'Rejected': 2}} with open('output.txt', 'w') as f: writer = csv.DictWriter(f, delimiter="\t", lineterminator="\n", fieldnames=['Date', 'Messaged', 'Rejected']) writer.writeheader() for k in sorted_metrics: d = {'Date': k} d.update(sorted_metrics.get(k)) writer.writerow(d) print(open('output.txt').read()) # Date Messaged Rejected # 1-Aug-2017 1 2 # 2-Aug-2017 3 2 # 3-Aug-2017 4 2 # 4-Aug-2017 2 2 # 5-Aug-2017 3 2 # 6-Aug-2017 1 2 # 7-Aug-2017 5 2 # 8-Aug-2017 0 2 # 9-Aug-2017 4 2 delimiter参数以匹配您想要的输出。