Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

write list of dicts to csv python

l = []

for dct in dict_list:
    l.append( pandas.DataFrame(dct).transpose() 
tmp = pandas.concat(l) # aggregate them all
tmp.to_csv('/my-file-path.csv')

#without pandas

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)
 
PREVIOUS NEXT
Tagged: #write #list #dicts #csv #python
ADD COMMENT
Topic
Name
3+5 =