Reputation:
I have a list of dictionaries:
[ defaultdict(<class 'dict'>,
{ 'account_id': '',
'address': { 'address_country': 'ZM',
'city': 'North Matthewland',
'state': 'Nevada',
'street_name': 'Cabrera Extensions',
'street_number': 197,
'zip_code': '81431'},
'affiliate_id': 12,
'brand': 'TTT',
'country': 'ZM',
'email': '[email protected]',
'first_name': 'Peter',
'last_name': 'Green',
'leadsource': 559,
'password': 'test385',
'phone_number': '052839601'},)]
In my situation, I need to put all this data to CSV file, so via csv module, I try to write all this data to CSV file but I receive every time error message:
ValueError: dict contains fields not in fieldnames: 'address'
So I add to 'fieldnames' address, but the problem is that I receive all data in one column of address.
with open('test_file.csv', 'w') as csvfile:
filed_names = ['first_name',
'last_name',
'email',
'phone_number',
'password',
'country',
'leadsource',
'affiliate_id',
'account_id',
'brand',
'street_number',
'street_name',
'city',
'state',
'address_country',
'zip_code',
]
writer = csv.DictWriter(csvfile, fieldnames=filed_names)
writer.writeheader()
writer.writerows(list_user_details)
Upvotes: 1
Views: 82
Reputation: 11
You'll need to flatten out the structure so it's just a list of dictionaries holding strings and numbers, rather than a list of dictionaries holding dictionaries of strings and numbers.
Assuming that you don't want to modify this list just for the writing step, make a new empty list. Copy the dictionaries from one list to the other, but convert the address
dictionary into a set of additional fields called address.address_country
, address.city
, address.state
and so on.
Then use csv
to write out this new list, and pass it the modified fieldnames.
Also don't forget to test with commas inside the field strings.
Upvotes: 1
Reputation: 164623
Pandas offers a convenient alternative:
# first flatten address
for d in lst:
d.update(d['address'])
del d['address']
# read into dataframe
df = pd.DataFrame(d, index=range(len(lst)))
# output to csv
df.to_csv('file.csv', index=False)
# Result
# account_id address_country affiliate_id brand city country \
# 0 ZM 12 TTT North Matthewland ZM
# email first_name last_name leadsource password phone_number \
# 0 [email protected] Peter Green 559 test385 052839601
# state street_name street_number zip_code
# 0 Nevada Cabrera Extensions 197 81431
Upvotes: 0