我的字典看起来像这样:
defaultdict(<type 'int'>, {'201304': defaultdict(<type 'int'>, {'District - 1': 20, 'District - 12': 9, 'District - 6': 5, 'District - 5': 1, 'District - 4': 1, 'District - 3': 1, 'District - 2': 13, 'District - 15': 1, 'District - 14': 2}), '201305': defaultdict(<type 'int'>, {'District - 1': 20, 'District - 12': 9, 'District - 6': 5, 'District - 5': 1, 'District - 4': 1, 'District - 3': 1, 'District - 2': 13, 'District - 15': 1, 'District - 14': 2}), '201306': defaultdict(<type 'int'>, {'District - 1': 20, 'District - 12': 9, 'District - 6': 5, 'District - 5': 1, 'District - 4': 1, 'District - 3': 1, 'District - 2': 13, 'District - 15': 1, 'District - 14': 2}), '201301': defaultdict(<type 'int'>, {'District - 1': 20, 'District - 12': 9, 'District - 6': 5, 'District - 5': 1, 'District - 4': 1, 'District - 3': 1, 'District - 2': 13, 'District - 15': 1, 'District - 14': 2}), '201302': defaultdict(<type 'int'>, {'District - 1': 20, 'District - 12': 9, 'District - 6': 5, 'District - 5': 1, 'District - 4': 1, 'District - 3': 1, 'District - 2': 13, 'District - 15': 1, 'District - 14': 2}), '201303': defaultdict(<type 'int'>, {'District - 1': 20, 'District - 12': 9, 'District - 6': 5, 'District - 5': 1, 'District - 4': 1, 'District - 3': 1, 'District - 2': 13, 'District - 15': 1, 'District - 14': 2}), '201212': defaultdict(<type 'int'>, {'District - 1': 20, 'District - 12': 9, 'District - 6': 5, 'District - 5': 1, 'District - 4': 1, 'District - 3': 1, 'District - 2': 13, 'District - 15': 1, 'District - 14': 2})})
我想要一个看起来像这样的简历:
month,District - 1,District -2, District -3...... for however many districts there are
201304,20,9,5,6,.....
我现在所拥有的是:
with open('output.csv','wb') as output_file:
w= csv.writer(output_file)
w.writerows(months.items())
months是我上面描述的词典的名称。不幸的是它输出:
201304 defaultdict(<type 'int'>, {'District - 1': 20, 'District - 12': 9, 'District - 6': 5, 'District - 5': 1, 'District - 4': 1, 'District - 3': 1, 'District - 2': 13, 'District - 15': 1, 'District - 14': 2})
201305 defaultdict(<type 'int'>, {'District - 1': 20, 'District - 12': 9, 'District - 6': 5, 'District - 5': 1, 'District - 4': 1, 'District - 3': 1, 'District - 2': 13, 'District - 15': 1, 'District - 14': 2})
201306 defaultdict(<type 'int'>, {'District - 1': 20, 'District - 12': 9, 'District - 6': 5, 'District - 5': 1, 'District - 4': 1, 'District - 3': 1, 'District - 2': 13, 'District - 15': 1, 'District - 14': 2})
201301 defaultdict(<type 'int'>, {'District - 1': 20, 'District - 12': 9, 'District - 6': 5, 'District - 5': 1, 'District - 4': 1, 'District - 3': 1, 'District - 2': 13, 'District - 15': 1, 'District - 14': 2})
201302 defaultdict(<type 'int'>, {'District - 1': 20, 'District - 12': 9, 'District - 6': 5, 'District - 5': 1, 'District - 4': 1, 'District - 3': 1, 'District - 2': 13, 'District - 15': 1, 'District - 14': 2})
201303 defaultdict(<type 'int'>, {'District - 1': 20, 'District - 12': 9, 'District - 6': 5, 'District - 5': 1, 'District - 4': 1, 'District - 3': 1, 'District - 2': 13, 'District - 15': 1, 'District - 14': 2})
201212 defaultdict(<type 'int'>, {'District - 1': 20, 'District - 12': 9, 'District - 6': 5, 'District - 5': 1, 'District - 4': 1, 'District - 3': 1, 'District - 2': 13, 'District - 15': 1, 'District - 14': 2})
我需要的是每个月1行:
month,District - 1,District -2, District - 3, .... (as many as show up)
201304,22,34,2,3,4,2,3,14
201305,34,22,1,3,5
201306,23,4,42,4,2,2,24,2,5,6,6,7,1
有关如何修改我的代码以执行此操作的任何建议吗?
答案 0 :(得分:2)
with open('output.csv','wb') as output_file:
w= csv.writer(output_file)
for month,values in months.iteritems():
for k,v in values.iteritems():
w.writerow([month, k ,v ] )
Update
对于内部字典的每个值,可以根据键值对其进行排序,而不是添加到输出文件
with open('output.csv','wb') as output_file:
w= csv.writer(output_file)
for month,values in months.iteritems():
sortedValue = [v[1] for v in sorted(values.items(),key = lambda x: x[0])
w.writerow([month] + sortedValue )
答案 1 :(得分:2)
您需要处理原始数据(a)以找出所有存在的区域,并(b)重新组织它,以便按月 - 区组合查找特定值。
raw_data = {
'201304': {'District - 1': 120, 'District - 12': 9, 'District - 6': 5, 'District - 5': 1, 'District - 4': 1, 'District - 3': 1, 'District - 2': 13, 'District - 15': 1, 'District - 14': 2},
'201305': {'District - 1': 220, 'District - 12': 9, 'District - 5': 1, 'District - 4': 1, 'District - 3': 1, 'District - 2': 13, 'District - 15': 1, 'District - 14': 2},
'201306': {'District - 1': 320, 'District - 12': 9, 'District - 6': 5, 'District - 5': 1, 'District - 4': 1, 'District - 3': 1, 'District - 2': 13, 'District - 15': 1, 'District - 14': 2},
'201301': {'District - 1': 420, 'District - 6': 5, 'District - 5': 1, 'District - 4': 1, 'District - 3': 1, 'District - 2': 13, 'District - 15': 1, 'District - 14': 2},
'201302': {'District - 1': 520, 'District - 12': 9, 'District - 6': 5, 'District - 5': 1, 'District - 4': 1, 'District - 3': 1, 'District - 2': 13, 'District - 15': 1, 'District - 14': 2},
'201303': {'District - 1': 620, 'District - 12': 9, 'District - 6': 5, 'District - 5': 1, 'District - 4': 1, 'District - 3': 1, 'District - 2': 13, 'District - 15': 1, 'District - 14': 2},
'201212': {'District - 1': 720, 'District - 6': 5, 'District - 5': 1, 'District - 4': 1, 'District - 3': 1, 'District - 2': 13, 'District - 15': 1, 'District - 14': 2},
}
# Reorganize data for lookup by (month, district) tuples,
# and determine all unique district names.
data = {}
districts = set()
for month, inner in raw_data.items():
for district, val in inner.items():
districts.add(district)
data[month, district] = val
districts = sorted(districts)
# Write data row-by-row. The CSV work should be straightforward
# at this point.
for month in sorted(raw_data):
row = [month]
row.extend(data.get((month, d), None) for d in districts)
print row
答案 2 :(得分:2)
您可以使用csv.DictWriter
,但需要添加月份列,因为它是一个二维字典:
#!python3
from collections import defaultdict
import csv
# populate the defaultdict "months"
# <deleted to keep short>
# Build a set of unique districts
s = set()
for k,v in months.items():
s.update(v.keys())
# open per requirements of csv.writer
with open('out.csv','w',newline='') as f:
# wrap in a DictWriter and specify column names
w = csv.DictWriter(f,fieldnames=['month']+list(sorted(s)))
w.writeheader()
for k,v in months.items():
# copy the dict of districts, add a month key and write the row
temp = dict(v)
temp['month'] = k
w.writerow(temp)
输出:
month,District - 1,District - 12,District - 14,District - 15,District - 2,District - 3,District - 4,District - 5,District - 6
201301,20,9,2,1,13,1,1,1,5
201302,20,9,2,1,13,1,1,1,5
201303,20,9,2,1,13,1,1,1,5
201304,20,9,2,1,13,1,1,1,5
201305,20,9,2,1,13,1,1,1,5
201306,20,9,2,1,13,1,1,1,5
201212,20,9,2,1,13,1,1,1,5
注意:如果使用Python 2,请改用以下open
:
with open('out.csv','wb') as f: