CodingNic

File I/O

File I/O with CSVs

File I/O 20 min read

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:

text
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:

text
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:

python
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:

python
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:

python
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:

python
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:

python
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

  1. Create a CSV with a header row and a few data rows, then read it back with csv.reader and print each row.
  2. Read that same file again with DictReader and print just one column’s values.
  3. Use csv.writer to 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.DictReader parse rows as lists or dictionaries; csv.writer/csv.DictWriter write 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.