我正在将多个 .csv 文件合并成一个字典,它看起来像这样(示例):
我希望将它保存为以下格式的 .csv 文件:
我现在拥有的代码片段(与此目标相关):
dtDict = {'AV-IM-1-13991730': {'6/1/2014 0:10': '0.96',
'6/1/2014 0:15': '0.92',
'6/1/2014 0:20': '0.97'},
'AV-IM-1-13991731': {'6/1/2014 0:10': '1.96',
'6/1/2014 0:15': '1.92',
'6/1/2014 0:20': '1.97'},
'AV-IM-1-13991732': {'6/1/2014 0:10': '2.96',
'6/1/2014 0:15': '2.92',
'6/1/2014 0:20': '2.97'},
'AV-IM-1-13991733': {'6/1/2014 0:10': '3.96',
'6/1/2014 0:15': '3.96',
'6/1/2014 0:20': '3.97'}}
我希望将它保存为以下格式的 .csv 文件:
timestamp,AV-IM-1-13991730,AV-IM-1-13991731,AV-IM-1-13991732,AV-IM-1-13991733
6/1/2014 0:10,0.96,1.96,2.96,3.96
6/1/2014 0:15,0.92,1.92,2.92,3.96
6/1/2014 0:20,0.97,1.97,2.97,3.97
我现在拥有的代码片段(与此目标相关):
header = '''# file...... Recorder file
# date...... Thu Mar 12 14:35:32 2015
# user...... Sri
# host...... (null)
# group..... None
# property.. AVA Measurements
# limit.....
# interval..'''
testpower = open("custpower.csv",'w')
testpower.writelines([header,'\n','# timestamp\n'])
...
for key, value in dtDict.iteritems():
#Still trying to figure out how to write to custpower.csv
我尝试着做了类似于这样的事情:
for key, value in dtDict.iteritems():
testpower.writelines([key,',',','.join(value),'\n'])
但它并没有完全做到我想要的。
[(时间戳,标题,数值) for 标题, d in data.items() for 时间戳, 数值 in d.items()]
,并从那里继续进行,但是我无法做出任何比罪孽还难看的东西。最后我想:“我是否已经足够熟悉 pandas,在这里将数据移动到它需要去的地方。” 结果所有数据都到了它应该去的地方——我很幸运! - Adam Smith