File I/O with CSVs
Objectives
By the end of this chapter, you should be able to:
- Explain what a CSV file is
- Read and write to CSV files
💡 Why this matters: CSVs are one of the most common formats for moving data between programs, spreadsheets, and databases. Reading and writing them programmatically is a skill you’ll use constantly.
What Is a CSV?
CSV stands for comma-separated values. CSV files can be opened in a plain text editor, or more commonly in spreadsheet programs like Excel or Google Sheets. Here’s an example, pets.csv:
name,type,age
Whiskey,dog,3
Moxie,cat,7
Mascis,dog,2
The comma isn’t required: many formats use other separators, called delimiters. A tab-separated file is called a TSV. Here’s the same data separated by | instead, in file.csv:
name|type|age
Whiskey|dog|3
Moxie|cat|7
Mascis|dog|2
Reading a CSV
Python’s csv module handles the parsing for you. You just tell it the delimiter:
import csv
with open("file.csv") as csvfile:
reader = csv.reader(csvfile, delimiter="|")
rows = list(reader)
for row in rows:
print(", ".join(row))
# name, type, age
# Whiskey, dog, 3
# Moxie, cat, 7
# Mascis, dog, 2
Each row comes back as a plain list of strings. If you’d rather work with dictionaries (one per row, keyed by column header), use DictReader instead:
import csv
with open("file.csv") as csvfile:
reader = csv.DictReader(csvfile, delimiter="|")
rows = list(reader)
for row in rows:
print(row)
# {'name': 'Whiskey', 'type': 'dog', 'age': '3'}
# {'name': 'Moxie', 'type': 'cat', 'age': '7'}
# {'name': 'Mascis', 'type': 'dog', 'age': '2'}
Note that every value comes back as a string, even age, since CSVs have no concept of data types. Convert with int() or float() yourself if you need numbers:
first_row_age = int(rows[0]["age"])
print(first_row_age, type(first_row_age)) # 3 <class 'int'>
Writing a CSV
Writing works the same way, but with a writer instead of a reader:
with open("file.csv", "a") as csvfile:
data_writer = csv.writer(csvfile, delimiter="|")
data_writer.writerow(["Bojack", "Horse", "50"])
And DictWriter for writing dictionaries, handy when you’re building a new CSV from scratch:
with open("newfile.csv", "a") as csvfile:
fieldnames = ["name", "fav_topic"]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader() # writes the column headings as the first row
writer.writerow({
"name": "Erin",
"fav_topic": "Writing to CSVs!"
})
To learn more about working with CSVs in Python, check the official csv module documentation.
Try It
- Create a CSV with a header row and a few data rows, then read it back with
csv.readerand print each row. - Read that same file again with
DictReaderand print just one column’s values. - Use
csv.writerto append a new row to the file, then confirm it’s there by reading the file again.
Recap
- A CSV stores tabular data as delimiter-separated text: commas by default, but any character works.
csv.reader/csv.DictReaderparse rows as lists or dictionaries;csv.writer/csv.DictWriterwrite them back out the same way.- Every value read from a CSV comes back as a string, regardless of what it looks like.
Next lesson: working with JSON files.