将列表中的字典转换为嵌套字典

Converting the dictionary inside the list to the nested dictionary

有一个元组列表,称为 employee_data,其中每个列表元素都是一个元组,对应于 class 和员工可以获得的积分。例如,

employee_data = 
[{
   "name": "asd",
   "lastname": "abc",
   "birthday": 15/15/2021,
   "birthplace": "CA",
   "live_place": "USA",
   "email": "sss.com",
   "website": "sss.com",
   "Phone_number": "12345678901",
   "work_number": "abc",
   "save_date": 15/15/2021,
   "start_date": 15/15/2021,
   "leave_date": 15/15/2021,
   "project": "End-Of-Support",
   "age_in_months": 256 ,
   "Age_in_Years": 15.3,
   "Computer_name": 'pc1',
   "computer_cpu": 8,
   "computer_ram": 12,
   "computer_ssd": 256,
 },
 {
   "name": "asd",
   "lastname": "abc",
   "birthday": 16/15/2021,
   "birthplace": "CA",
   "live_place": "USA",
   "email": "sss.com",
   "website": "sss.com",
   "Phone_number": "12345678901",
   "work_number": "abc",
   "save_date": 15/15/2021,
   "start_date": 15/15/2021,
   "leave_date": 15/15/2021,
   "project": "End-Of-Support",
   "age_in_months": 256 ,
   "Age_in_Years": 15.3,
   "Computer_name": 'pc1',
   "computer_cpu": 8,
   "computer_ram": 12,
   "computer_ssd": 256,
 }]
Nested Dict ID name lastname birthday birthplace live_place email
0 asd abc 15/15/2021 USA CAD asd@mail.com
1 asd2 abc 16/15/2021 CAD USA abc@mail.com

我尝试了这个功能,但无法修复错误。 这里是 a link!

我的问题是,列表中有这本词典。我想为映射数据创建嵌套字典。

[{0{"name": "asd",
   "lastname": "abc",
   "birthday": 15/15/2021,
   "birthplace": "CA",
   "live_place": "USA",
   "email": "sss.com",}
1{"name": "asd2",
   "lastname": "abc",
   "birthday": 16/15/2021,
   "birthplace": "CA",
   "live_place": "USA",
   "email": "sss.com",}]

如果员工姓氏相同,我想获取员工 nestedDictID,然后我将导入不同列表中的所有信息,table ..

d = { x['lastname']: x['abc'] for x in employee_data}
KeyError: 'abc'

您可以创建一个元组列表,其中包含从 json 中提取的所需数据,您可以使用这些数据将其导出到 SQL 表中。参考以下代码:

import pandas as pd
data = [(item.get('name'), item.get('lastname'),item.get('birthday'),item.get('birthplace'), item.get('live_place'), item.get('email')) for item in employee_data]
print(data)

df = pd.DataFrame.from_records(data, columns=['name', 'lastname', 'birthday','birthplace','live_place','email']))

然后您可以使用 pandas 中的 df.to_sql