Search
 
SCRIPT & CODE EXAMPLE
 

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

PREVIOUS NEXT
Code Example
Python :: python disable warning deprecated 
Python :: get n items from dictionary python 
Python :: how to multiply two tuples in python 
Python :: python current utc offset 
Python :: show a image in python 
Python :: how to get what type of file in python 
Python :: Python find max in list of dict by value 
Python :: read csv and set column name in pandas 
Python :: finding the format of an image in cv2 
Python :: creating folder in s3 bucket python 
Python :: python not null 
Python :: perfect number program in python 
Python :: replace value column by another if missing pandas 
Python :: panda check a cell value is not a number 
Python :: parameter grid 
Python :: make longitude -180 to 180 
Python :: how to convert input to uppercase in python 
Python :: how to convert png to pdf with python 
Python :: remove last element from dictionary python 
Python :: read csv without index 
Python :: pandas read chunk of csv 
Python :: getting pi in python 
Python :: pandas to csv float format 
Python :: linkedin dynamic scrolling using selenium python 
Python :: define variable with if statement python 
Python :: python - exchange rate API 
Python :: django phone number field 
Python :: sort tuple list python 
Python :: how to read files in python 
Python :: csv reader python skip header 
ADD CONTENT
Topic
Content
Source link
Name
1+5 =