【问题标题】:Write list of dictionaries with additional lists to CSV - Python [closed]将带有附加列表的字典列表写入CSV - Python [关闭]
【发布时间】:2021-09-05 01:25:03
【问题描述】:

我环顾四周,但似乎没有其他线程能完全回答我面临的具体挑战。

例如,this thread 告诉我如何将字典列表写入 CSV。

This one 解释了当每个键的值是一个列表时如何编写字典。

我有一个需要写入 CSV 的字典列表,其中只有一个值是一个列表。例如:

[{
    'name': 'name_1', 
    'id': 'id_1', 
    'info': [{
        'info_1': 'some info',
        'info_2': 'more info'
        },
        {
        'info_1': 'all the info',
        'info_2': 'extra info'
    }]
 },
 {    
    'name': 'name_2', 
    'id': 'id_2', 
    'info': [{
        'info_1': 'another piece of info the same type as info_1 above',
        'info_2': 'info'
        },
        {
        'info_1': 'getting tedious',
        'info_2': 'you get the picture...'
        }
    ]
}]

输出标题是: 姓名、身份证、info_1、info_2

【问题讨论】:

  • 您希望您的 csv 看起来像什么?您能否根据您的字典列表更新您的答案以包含示例输出。
  • 道歉。更新了我的答案

标签: python list csv dictionary


【解决方案1】:

要从提供的列表创建 CSV 文件,您可以使用以下示例:

import csv

lst = [
    {
        "name": "name_1",
        "id": "id_1",
        "info": [
            {"info_1": "some info", "info_2": "more info"},
            {"info_1": "all the info", "info_2": "extra info"},
        ],
    },
    {
        "name": "name_2",
        "id": "id_2",
        "info": [
            {
                "info_1": "another piece of info the same type as info_1 above",
                "info_2": "info",
            },
            {"info_1": "getting tedious", "info_2": "you get the picture..."},
        ],
    },
]
with open("data.csv", "w") as f_out:
    writer = csv.writer(f_out)
    writer.writerow(["name", "id", "info_1", "info_2"])
    for d in lst:
        for i in d["info"]:
            writer.writerow([d["name"], d["id"], i["info_1"], i["info_2"]])

创建 data.csv(来自 LibreOffice 的屏幕截图):

【讨论】:

    【解决方案2】:

    这种类型的字典列表称为嵌套JSON;最好使用 Pandas 方法 json_normalize 将此数据类型处理为 CSV :

    import pandas as pd
    
    test = [{
        'name': 'name_1', 
        'id': 'id_1', 
        'info': [{
            'info_1': 'some info',
            'info_2': 'more info'
            },
            {
            'info_1': 'all the info',
            'info_2': 'extra info'
        }]
     },
     {    
        'name': 'name_2', 
        'id': 'id_2', 
        'info': [{
            'info_1': 'another piece of info the same type as info_1 above',
            'info_2': 'info'
            },
            {
            'info_1': 'getting tedious',
            'info_2': 'you get the picture...'
            }
        ]
    }]
    
    df = pd.json_normalize(test, 'info', ['id', 'name'], 
                        record_prefix='information_')
    
    df.to_csv('information.csv')
    

    最终结果(截图):

    *:对于这个问题,最好使用 Pandas 而不是 CSV 标准库,因为如果你有一个大文件,处理这个问题的 2 次迭代需要很多时间处理。

    【讨论】:

    • 啊,这太完美了!我知道 pandas json_normalize 函数,但不知道如何处理额外的嵌套值。谢谢。
    • 查看这门课程,关于嵌套 JSON 的一些有趣的事情,Data Camp Nested JSON
    猜你喜欢
    • 2016-03-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-07
    • 2020-10-14
    相关资源
    最近更新 更多