To write CSVs in Python, you’ll want the builtin csv
module. More specifically, I usually use csv.DictWriter
.
Python DictWriter
Python’s csv
module has both a writer
and a DictWriter
class, but I’m virtually always working with dictionaries, so I always use DictWriter
.
It’s pretty straightforward. You grab your data, open a file in a context manager, create a writer
, and write:
from csv import DictWriter
nic_cage = {
"first_name": "Nicolas",
"last_name": "Cage",
"oscars": 1, # yep, he won one
"description": "was in National Treasure",
}
tom_hanks =
"first_name": "Tom",
"last_name": "Hanks",
"oscars": 2,
"description": "IS a national treasure",
}
actors = [nic_cage, tom_hanks]
# Open a file in write mode
with open("actors.csv", "w") as outfile:
# The dict keys will be the csv headers
headers = tom_hanks.keys()
# create a writer
writer = DictWriter(outfile, fieldnames=headers)
# write the header row
writer.writeheader()
# write the rest of the rows
writer.writerows(actors)
That’s it! Now you have a csv with a header row and two entries.
Important note: if any of your dicts have additional keys that aren’t in your defined set of headers
, this will fail with ValueError: dict contains field not in fieldnames
:
# This example will fail to write with a ValueError
from csv import DictWriter
tom_hanks = { "first_name": "Tom", "oscars": 1, "national_treasure": True }
nic_cage = { "first_name": "Nicolas", "oscars": 1 }
with open("actors.csv", "w") as outfile:
# tom_hanks has an additional key in his data
# This csv will fail to write
headers = ["first_name", "oscars"]
writer = DictWriter(outfile, fieldnames=headers)
writer.writeheader()
writer.writerows([tom_hanks, nic_cage])
# ValueError: dict contains field not in fieldnames: 'national_treasure'
However, if any of your dicts is missing a key which was defined in your headers, that piece of data will just be blank:
# This example will save fine, but with some blank data
from csv import DictWriter
tom_hanks = { "first_name": "Tom", "oscars": 2 }
meryl_streep = { "first_name": "Meryl", "last_name": "Streep" "oscars": 3 }
with open("actors.csv", "w") as outfile:
# tom_hanks is missing "last_name" above
# This csv will save fine, but his last_name will be blank
headers = ["first_name", "oscar_count", "last_name"]
writer = DictWriter(outfile, fieldnames=headers)
writer.writeheader()
writer.writerows([tom_hanks, meryl_streep])
Easy.
Alternative Option: Pandas
The most often-used alternative is likely pandas data_frame.to_csv()
. If you do any kind of data analysis, you’re probably familiar with pandas
, so I’ll just do a quick to_csv
example:
import pandas as pd
# Pandas works on DataFrames, let's build a tiny one
df = pd.DataFrame(
[["Tom", "Hanks"], ["Meryl", "Streep"]],
columns=["first_name", "last_name"],
)
"""
df looks like this
first_name last_name
0 Tom Hanks
1 Meryl Streep
"""
# You'll probably want index=false, or your CSV will save
# the row indices 0 and 1 in an unnamed first column
df.to_csv("actors.csv", index=false)
All done. Pandas saved a CSV with headers.
Helpful Links
- csv.DictWriter – Python official docs
- Python Context Managers – Me!
- Reading CSVs in Python – Also me!