如何将字典列表转换为表格或csv文件?

时间:2019-03-31 18:51:24

标签: python json csv dictionary python-3.7

无法将Python字典转换为表格,然后将数据导出到csv。

dict string: {"test_sheet": {"testheader": [{"2018-12-31": {"field1": 8482000000, "field2": 166731000000, "field3": 92128000000}}, {"2018-11-30": {"field1": 7579000000, "field2": 171652000000, "field3": 85967000000}}, {"2018-10-31": {"field1": 8053000000, "field2": 176130000000, "field3": 82718000000}}, {"2018-09-30": {"field1": 8544000000, "field2": 166258000000, "field3": 79239000000}}]}}

Format of table needed:
Report     Name       Date       Field1     Field2       Field3
test_sheet testheader 31.12.2018 8482000000 166731000000 92128000000
test_sheet testheader 30.11.2018 7579000000 171652000000 85967000000
test_sheet testheader 31.10.2018 8053000000 176130000000 82718000000
test_sheet testheader 30.09.2018 8544000000 166258000000 79239000000

尝试使用read_json将dict转换为csv

import pandas
data = {"test_sheet": {"testheader": [{"2018-12-31": {"field1": 8482000000, "field2": 166731000000, "field3": 92128000000}}, {"2018-11-30": {"field1": 7579000000, "field2": 171652000000, "field3": 85967000000}}, {"2018-10-31": {"field1": 8053000000, "field2": 176130000000, "field3": 82718000000}}, {"2018-09-30": {"field1": 8544000000, "field2": 166258000000, "field3": 79239000000}}]}}

pandas.read_json(json.dumps(data)).to_csv('testfile.csv')

但是在导出到csv之后,所有数据保存在第一行。

新的详细输入数据:

{"test_sheet": {"testheader": [ {"2018-12-31": {"field1": 8482000000, "field2": 166731000000, "field3": 92128000000}}, {"2018-11-30": {"field1": 7579000000, "field2": 171652000000, "field3": 85967000000, "field4": 6679000000, "field5": 159000000}}, {"2018-10-31": {"field1": 8053000000, "field2": 176130000000, "field3": 82718000000, "field4": 1218000000}}, {"2018-09-30": {"field1": 8544000000, "field2": 166258000000, "field3": 79239000000}}], "testheader1": [ {"2018-12-31": {"field1": 8482000000, "field2": 166731000000, "field3": 92128000000, "field4": 124000000}}, {"2018-11-30": {"field1": 7579000000, "field2": 171652000000, "field3": 85967000000, "field4": 44367000000, "field5": 582000000}}, {"2018-10-31": {"field1": 8053000000, "field2": 176130000000, "field3": 82718000000, "field4": 132500000, "field5": 15847000, "field6": 1982330000}}, {"2018-09-30": {"field1": 8544000000, "field2": 166258000000, "field3": 79239000000}}]}}

此数据所需的输出格式:

Report      Name        Date       FieldName FieldValue
test_sheet  testheader  31.12.2018  Field1  8482000000
test_sheet  testheader  31.12.2018  Field2  166731000000
test_sheet  testheader  31.12.2018  Field3  92128000000
test_sheet  testheader  30.11.2018  Field1  7579000000
test_sheet  testheader  30.11.2018  Field2  171652000000
test_sheet  testheader  30.11.2018  Field3  85967000000
test_sheet  testheader  30.11.2018  Field4  6679000000
test_sheet  testheader  30.11.2018  Field5  159000000
test_sheet  testheader  31.10.2018  Field1  8053000000
test_sheet  testheader  31.10.2018  Field2  176130000000
test_sheet  testheader  31.10.2018  Field3  82718000000
test_sheet  testheader  31.10.2018  Field4  1218000000
test_sheet  testheader  30.09.2018  Field1  8544000000
test_sheet  testheader  30.09.2018  Field2  166258000000
test_sheet  testheader  30.09.2018  Field3  79239000000
test_sheet  testheader1 31.12.2018  Field1  8482000000
test_sheet  testheader1 31.12.2018  Field2  166731000000
test_sheet  testheader1 31.12.2018  Field3  92128000000
test_sheet  testheader1 31.12.2018  Field4  124000000
test_sheet  testheader1 30.11.2018  Field1  7579000000
test_sheet  testheader1 30.11.2018  Field2  171652000000
test_sheet  testheader1 30.11.2018  Field3  85967000000
test_sheet  testheader1 30.11.2018  Field4  44367000000
test_sheet  testheader1 30.11.2018  Field5  582000000
test_sheet  testheader1 31.10.2018  Field1  8053000000
test_sheet  testheader1 31.10.2018  Field2  176130000000
test_sheet  testheader1 31.10.2018  Field3  82718000000
test_sheet  testheader1 31.10.2018  Field4  132500000
test_sheet  testheader1 31.10.2018  Field5  15847000
test_sheet  testheader1 31.10.2018  Field6  1982330000
test_sheet  testheader1 30.09.2018  Field1  8544000000
test_sheet  testheader1 30.09.2018  Field2  166258000000
test_sheet  testheader1 30.09.2018  Field3  79239000000

2 个答案:

答案 0 :(得分:0)

数据集过于定制,无法与某些框架一起使用。这是一种方法:

import csv

data = {"test_sheet": {"testheader": [{"2018-12-31": {"field1": 8482000000, "field2": 166731000000, "field3": 92128000000}}, {"2018-11-30": {"field1": 7579000000, "field2": 171652000000, "field3": 85967000000}}, {"2018-10-31": {"field1": 8053000000, "field2": 176130000000, "field3": 82718000000}}, {"2018-09-30": {"field1": 8544000000, "field2": 166258000000, "field3": 79239000000}}]}}
pf = open("out.csv", "w")
writer = csv.DictWriter(pf, fieldnames=["Report", "Name", "Date", "Field1", "Field2", "Field3"])

writer.writeheader()

for report, report_data in data.items():
    for name, name_data in report_data.items():
        for date_wrapper in name_data:
            date = list(date_wrapper.keys())[0]
            date_data = date_wrapper[date]
            writer.writerow({
                "Report": report,
                "Name": name,
                "Date": date,
                "Field1": date_data['field1'],
                "Field2": date_data['field2'],
                "Field3": date_data['field3']
            })

pf.close()

更新:对于第二个版本:

import csv

data = {"test_sheet": {"testheader": [ {"2018-12-31": {"field1": 8482000000, "field2": 166731000000, "field3": 92128000000}}, {"2018-11-30": {"field1": 7579000000, "field2": 171652000000, "field3": 85967000000, "field4": 6679000000, "field5": 159000000}}, {"2018-10-31": {"field1": 8053000000, "field2": 176130000000, "field3": 82718000000, "field4": 1218000000}}, {"2018-09-30": {"field1": 8544000000, "field2": 166258000000, "field3": 79239000000}}], "testheader1": [ {"2018-12-31": {"field1": 8482000000, "field2": 166731000000, "field3": 92128000000, "field4": 124000000}}, {"2018-11-30": {"field1": 7579000000, "field2": 171652000000, "field3": 85967000000, "field4": 44367000000, "field5": 582000000}}, {"2018-10-31": {"field1": 8053000000, "field2": 176130000000, "field3": 82718000000, "field4": 132500000, "field5": 15847000, "field6": 1982330000}}, {"2018-09-30": {"field1": 8544000000, "field2": 166258000000, "field3": 79239000000}}]}}
pf = open("out.csv", "w")
writer = csv.DictWriter(pf, fieldnames=["Report", "Name", "Date", "FieldName", "FieldValue"])

writer.writeheader()

for report, report_data in data.items():
    for name, name_data in report_data.items():
        for date_wrapper in name_data:
            date = list(date_wrapper.keys())[0]
            date_data = date_wrapper[date]

            for field_name, field_value in date_data.items():
                writer.writerow({
                    "Report": report,
                    "Name": name,
                    "Date": date,
                    "FieldName": field_name,
                    "FieldValue": field_value
                })

pf.close()

答案 1 :(得分:0)

您的数据格式非常嵌套。 CSV不能很好地处理嵌套结构。

您提供的代码将起作用-如果您事先进行了一些数据预处理。 每一行都可以按以下方式访问:data["test_sheet"]["test_header"][i] 像这样访问每一行并向其中添加前两列。