Python Dictionary to CSV

Sample data:

mydict = [{"col1": 1000, "col2": 2000}, {"col1": 3000, "col2": 4000}]

One-liner for converting a list of dicts to CSV, using pandas:

import pandas as pd

pd.DataFrame(mydict).to_csv('out.csv', index=False)

Results:

col1,col2
1000,2000
3000,4000

Leave a Comment