How to Convert This List of Dictionaries to a CSV File

How do I convert this list of dictionaries to a csv file?

import csv

to_csv = [
{'name': 'bob', 'age': 25, 'weight': 200},
{'name': 'jim', 'age': 31, 'weight': 180},
]

keys = to_csv[0].keys()

with open('people.csv', 'w', newline='') as output_file:
dict_writer = csv.DictWriter(output_file, keys)
dict_writer.writeheader()
dict_writer.writerows(to_csv)

Convert list of dicts to CSV in Python 3

Use DicitWritter() restval parameter,

The optional restval parameter specifies the value to be written if
the dictionary is missing a key in fieldnames.

and for fieldnames parameter use a list of all available keys in a list of dictionaries.

import csv

listOfDicts = [
{'key1': 'value1', 'key3':'value3'},
{'key1': 'someValue', 'key2':'value2', 'key3':'value3'},
{'anotherKey': 'anotherValue', 'key1': 'value1', 'key2':'value2'},
{'anotherKey': 'anotherValue', 'anotherKey1': 'anotherValue1', 'key1': 'value1', 'key2':'value2', 'key3':'value3'},
]

keys = [i for s in [d.keys() for d in listOfDicts] for i in s]

with open('test.csv', 'a') as output_file:
dict_writer = csv.DictWriter(output_file, restval="-", fieldnames=keys, delimiter='@')
dict_writer.writeheader()
dict_writer.writerows(listOfDicts)

output:

$ cat test.csv 
key3@key1@key2@anotherKey@anotherKey1
value3@value1@-@-@-
value3@someValue@value2@-@-
-@value1@value2@anotherValue@-
value3@value1@value2@anotherValue@anotherValue1

Reference: https://docs.python.org/2/library/csv.html#csv.DictWriter

Convert list of dictionaries to csv file

this should do the trick :)

data = [ {'site1':'data1'}, {'site2':'data2'} ]

with open ('list.csv', 'w') as f:
for dict in data:
for key, value in dict.items():
text = key+','+value+'\n'
f.writelines(text)

convert list of dict to CSV string using dict_keys

csv.DictWriter

csv module provides a DictWriter class which is best suited when we are dealing with records i.e. list of dictionaries that needs to be written to a csv file

fields = ['fruit', 'count', 'color'] 
writer = csv.DictWriter(output, fieldnames=fields, delimiter='\t')
writer.writeheader()
writer.writerows(csvdata)


print(output.getvalue())

fruit count color
apple 1 red
banana 2 yellow

List of list of dictionaries to CSV

Without Pandas, you can use itertools.chain to get a flattened list of all dictionaries and then write that to your CSV file with csv.DictWriter:

import csv
from itertools import chain

data = [
[{'e': 46, 'p': 100, 'n': 0, 'a': 100},
{'e': 29, 'p': 40, 'n': 1, 'a': 40}],
[{'e': 56, 'p': 200, 'n': 23, 'a': 10},
{'e': 22, 'p': 41, 'n': 11, 'a': 420}]]

fieldnames = ['e', 'p', 'n', 'a']
with open('mydata.csv', 'w') as f:
writer = csv.DictWriter(f, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(chain.from_iterable(data))

Output (mydata.csv)

e,p,n,a
46,100,0,100
29,40,1,40
56,200,23,10
22,41,11,420

CSV from list of dictionaries with differing length and keys

The problem is that you will need the full column set to write the header at the beginning of the file. But apart from that, csv.DictWriter is what you need:

# optional: compute the fieldnames:
fieldnames = set()
for d in dict_list:
fieldnames.update(d.keys())
fieldnames = sorted(fieldnames) # sort the fieldnames...

# produce the csv file
with open("file.csv", "w", newline='') as fd:
wr = csv.DictWriter(fd, fieldnames)
wr.writeheader()
wr.writerows(dict_list)

And the produced csv will look like this:

A,B,C,D,E
1,2,,,
,,3,4,5
,,6,7,8

If you really want to combine rows with disjoint set of keys, you could do:

# produce the csv file
with open("file.csv", "w", newline='') as fd:
wr = csv.DictWriter(fd, sorted(fieldnames))
old = { k: k for k in wr.fieldnames } # use old for the header line
for row in dict_list:
if len(set(old.keys()).intersection(row.keys())) != 0:
wr.writerow(old) # common fields: write old and start a new row
old = row
old.update(row) # disjoint fields: just combine
wr.writerow(old) # do not forget last row

You would get:

A,B,C,D,E
1,2,3,4,5
,,6,7,8

Write list of dictionaries with additional lists to CSV - Python

This type of list of the dictionary is called Nested JSON; it is better to handle this data type to CSV with Pandas method json_normalize :

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

final result(screenshot):
Office CSV reader

*: it is better to use Pandas for this problem instead of CSV standard library because if you have a large file, 2 iteration to handle this problem has much time processing.

How to convert a list of dictionaries into a csv?

You could use the csv library

import csv

dict_list=[{'Country':'US', 'Industry':'Telecom', 'Score':105},
{'Country':'US', 'Industry':'Banking', 'Score':145}]

with open('names.csv', 'w') as csvfile:
fieldnames = ['Country', 'Industry', 'Score']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames, delimiter='\t')

writer.writeheader()
for d in dict_list:
writer.writerow(d)

Or more compactly.

with open('names.csv', 'w') as csvfile:
fieldnames = ['Country', 'Industry', 'Score']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames, delimiter='\t')

writer.writeheader()
writer.writerows(dict_list)

plus: Assuming that each element in the list contains all the corresponding elements we can use the following code to obtain the header. Thanks @martineau

fieldnames = dict_list[0].keys() 

Convert list of dict to csv in python

use csv module of python.

data_list = [{...},{...}...]

keys = data_list[0].keys()
with open('test.csv', 'wb') as output_file:
dict_writer = csv.DictWriter(output_file, keys)
dict_writer.writeheader()
dict_writer.writerows(data_list)


Related Topics



Leave a reply



Submit