【问题标题】:How do I combine two python dictionaries and create a csv file?如何组合两个 python 字典并创建一个 csv 文件?
【发布时间】:2021-11-25 07:34:01
【问题描述】:

我有两个包含会议详细信息/参与者的字典。如何组合这两个字典,以便在一个 .csv 文件中只显示两个字典的参与者信息?

import pandas as pd
import json

meeting1 = {
    'other': 'info',
    'participants' : [ 
        {
            'id': '123',
            'email': '1@email.com',
            'room': '1a'
        },
        {
            'id': '124',
            'email': '2@email.com',
            'room': '1a'
        }
    ]
}

meeting2 = {
    'other': 'info',
    'participants' : [ 
        {
            'id': '125',
            'email': '3@email.com',
            'room': '1b'
        }
    ]
}

particpants =  {key:[meeting1[key], meeting2[key]] for key in meeting1}


print(particpants)
 
json_object = json.dumps(particpants) 
print(json_object)

df = pd.read_json(json_object)
df.to_csv('participants.csv')

这是我在 .csv 文件中的当前输出:

other,participants
info,"[{'id': '123', ... 'room': '1a'}, {'id': '124', ... 'room': '1a'}]"
info,"[{'id': '125', ... 'room': '1b'}]"

这是我试图在 .csv 文件中获取的内容:

id, email, room
123, 1@email.com, 1a
124, 2@email.com, 1a
125, 3@email.com, 1b

【问题讨论】:

  • 仅供参考:彻底回答问题非常耗时。 如果您的问题得到解决,请通过接受最适合您的需求的解决方案表示感谢。 接受检查位于答案左上角的向上/向下箭头下方。如果出现更好的解决方案,则可以接受新的解决方案。您还可以使用向上或向下箭头对答案的质量/有用性进行投票。 如果解决方案不能回答问题,请发表评论。 What should I do when someone answers my question?。谢谢

标签: python json pandas dictionary


【解决方案1】:

这里是只使用 pandas 的解决方案。

df = pd.concat(
    [pd.DataFrame(meeting1["participants"]),
     pd.DataFrame(meeting2["participants"])],
    ignore_index=True)

输出

    id        email room
0  123  1@email.com   1a
1  124  2@email.com   1a
2  125  3@email.com   1b

你可以使用df.to_csv("file.csv")保存到csv

【讨论】:

    【解决方案2】:

    你可以在没有 pandas 或 json 的情况下做到这一点。

    import csv
    
    meeting1 = {
        'other': 'info',
        'participants' : [ 
            {
                'id': '123',
                'email': '1@email.com',
                'room': '1a'
            },
            {
                'id': '124',
                'email': '2@email.com',
                'room': '1a'
            }
        ]
    }
    
    meeting2 = {
        'other': 'info',
        'participants' : [ 
            {
                'id': '125',
                'email': '3@email.com',
                'room': '1b'
            }
        ]
    }
    
    # Create a dictionary mapping the participant ID, which I assume is unique
    # to the participant details
    # That participants in both meetings are not duplicated
    all_participants = {p["id"]: p for mtg in (meeting1, meeting2) for p in mtg["participants"] }
    
    with open("out.csv", "wt", newline="") as fd:
        wrtr = csv.DictWriter(fd, fieldnames=["id", "email", "room"})
        wrtr.writeheader()
        wrtr.writerows(all_participants.values())
    

    【讨论】:

      猜你喜欢
      • 2019-03-27
      • 1970-01-01
      • 1970-01-01
      • 2012-08-12
      • 2020-03-18
      • 1970-01-01
      • 2017-05-01
      • 2017-07-25
      • 2012-02-10
      相关资源
      最近更新 更多