How do I convert this list of dictionaries to a csv file?
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 2 Looping
--
Chapters
00:00 Question
00:27 Accepted answer (Score 445)
00:43 Answer 2 (Score 35)
02:43 Answer 3 (Score 18)
03:01 Answer 4 (Score 9)
03:28 Thank you
--
Full question
https://stackoverflow.com/questions/3086...
Answer 1 links:
[Preserving column order in Python Pandas DataFrame]: https://stackoverflow.com/questions/1565...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #csv #dictionary #dataconversion
#avk47
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 2 Looping
--
Chapters
00:00 Question
00:27 Accepted answer (Score 445)
00:43 Answer 2 (Score 35)
02:43 Answer 3 (Score 18)
03:01 Answer 4 (Score 9)
03:28 Thank you
--
Full question
https://stackoverflow.com/questions/3086...
Answer 1 links:
[Preserving column order in Python Pandas DataFrame]: https://stackoverflow.com/questions/1565...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #csv #dictionary #dataconversion
#avk47
ACCEPTED ANSWER
Score 481
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)
ANSWER 2
Score 39
In python 3 things are a little different, but way simpler and less error prone. It's a good idea to tell the CSV your file should be opened with utf8 encoding, as it makes that data more portable to others (assuming you aren't using a more restrictive encoding, like latin1)
import csv
toCSV = [{'name':'bob','age':25,'weight':200},
{'name':'jim','age':31,'weight':180}]
with open('people.csv', 'w', encoding='utf8', newline='') as output_file:
fc = csv.DictWriter(output_file,
fieldnames=toCSV[0].keys(),
)
fc.writeheader()
fc.writerows(toCSV)
- Note that
csvin python 3 needs thenewline=''parameter, otherwise you get blank lines in your CSV when opening in excel/opencalc.
Alternatively: I prefer use to the csv handler in the pandas module. I find it is more tolerant of encoding issues, and pandas will automatically convert string numbers in CSVs into the correct type (int,float,etc) when loading the file.
import pandas
dataframe = pandas.read_csv(filepath)
list_of_dictionaries = dataframe.to_dict('records')
dataframe.to_csv(filepath)
Note:
- pandas will take care of opening the file for you if you give it a path, and will default to
utf8in python3, and figure out headers too. - a dataframe is not the same structure as what CSV gives you, so you add one line upon loading to get the same thing:
dataframe.to_dict('records') - pandas also makes it much easier to control the order of columns in your csv file. By default, they're alphabetical, but you can specify the column order. With vanilla
csvmodule, you need to feed it anOrderedDictor they'll appear in a random order (if working in python < 3.5). See: Preserving column order in Python Pandas DataFrame for more.
ANSWER 3
Score 19
this is when you have one dictionary list:
import csv
with open('names.csv', 'w') as csvfile:
fieldnames = ['first_name', 'last_name']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerow({'first_name': 'Baked', 'last_name': 'Beans'})
ANSWER 4
Score 9
Because @User and @BiXiC asked for help with UTF-8 here a variation of the solution by @Matthew. (I'm not allowed to comment, so I'm answering.)
import unicodecsv as csv
toCSV = [{'name':'bob','age':25,'weight':200},
{'name':'jim','age':31,'weight':180}]
keys = toCSV[0].keys()
with open('people.csv', 'wb') as output_file:
dict_writer = csv.DictWriter(output_file, keys)
dict_writer.writeheader()
dict_writer.writerows(toCSV)