0

have a list of tuples, called employee_data, where each list element is a tuple that corresponds to a class and a point that a employee can earn. For example,

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

I tried this functions but couldn't fix error. Here's a link!

My issue is, this dictionary in list. I want to create nested dict for mapping datas.

[{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",}]

if employee has same last name i want to get employee nestedDictID than i'll import all infomartion on different list and table ..

d = { x['lastname']: x['abc'] for x in employee_data}
KeyError: 'abc'
sapkernel
  • 3
  • 2
  • 1
    Hello, so what is your question? – gremur Mar 22 '22 at 16:59
  • That's not a list of tuples. That's a list of dictionaries. – Tim Roberts Mar 22 '22 at 17:26
  • please show what you tried and explain how it doesn't work with lots of data? also, the question title asks about printing data, but the body mentions importing to sql; focus on just one of those – ysth Mar 22 '22 at 17:32
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Mar 22 '22 at 19:09

1 Answers1

0

You can create a list of tuples with the required data extracted from json which you can use to export it to SQL tables. Refer the below code:

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']))

And then you can use df.to_sql from pandas.

Vaibhav Jadhav
  • 2,020
  • 1
  • 7
  • 20