python - 以特定格式将 "dictionary of dictionaries"写入 .csv 文件

标签 python csv dictionary

我正在从多个 .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'])

但它并没有完全完成我想做的事情。

最佳答案

如果您可以使用 pandas,这将非常简单。

import pandas as pd

data = {'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'}}

df = pd.DataFrame(data)
df.to_csv(PATH_TO_OUTPUT_FILE)

df 变成了一个看起来像的DataFrame

              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

你得到的 csv 看起来像

,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

Pandas 也很不错,因为你可以这样做:

df.convert_objects(convert_numeric=True).plot()
# the converts change "0.97" -> 0.97 so it's plottable

得到:

Dataframe

关于python - 以特定格式将 "dictionary of dictionaries"写入 .csv 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31436783/

相关文章:

python - 如何使用allauth将范围参数设置为 strip 上的read_write?

python - 尝试使用 matplotlib 绘制 OpenCV 的 MSER 区域

arrays - 将csv转换为不同的格式

javascript - 在数字排序期间保持精度

javascript - 如何将折线添加到 mapbox.js map ?

python - 无法通过 ftplib (Python) 使用 IP 地址

python - WSGI/Django : pass username back to Apache for access log

csv - ArangoDB 将 csv 导入边缘(图表)

r - Google 文档导出带有逗号的电子表格值。 R 中的 read.csv() 将这些视为因素而不是数字

python - 如何使用 Python 按字母数字顺序按值对字典进行排序?