CVS Files
Instruction
Spreadsheets weren’t designed as databases. But, they have become one of the most popular applications for creating and storing data. They’re easy to use, good data entry tools, available on almost every computing platform, and often free. They enable users to perform calculations, analyze, and visualize data. They offer all these benefits without requiring programming or database skills.
Because of this, you’ll often work with data in CSV (comma-separated value) format. Although it’s been associated with spreadsheets since the 1980s, it’s been around since computing’s earliest days.
Basic CSV File Reading
To work with CSV data, you need to import the csv module, which is part of the Python Standard Library.
Since CSV files are files, you create a file object as you would when opening a text file. But then you pass it as an argument to the csv.reader() method, which creates an iterator that you can use in a for loop:
import csv
with open("some-file.csv", "r") as file:
reader = csv.reader(file)
for row in reader:
print(row)
The code above prints each row as a list of strings. For example, if you had created the CSV file, some-file.csv, which contained the following:
browser,vendor,year_appeared
Chrome,Google,2008
Safari,Apple,2003
Edge,Microsoft,2015
The code’s output would look like this:
['browser', 'vendor', 'year_appeared']
['Chrome', 'Google', '2008']
['Safari', 'Apple', '2003']
['Edge', 'Microsoft', '2015']
Basic CSV File Writing
Reading a CSV file involves creating a file object and then passing it to csv.reader(). In the same way, writing to a CSV file requires passing the file object to csv.writer(). This creates a CSV writer object that writes to the file in CSV format.
The writer object has these methods for writing to a CSV file:
-
writerow(): Takes a list and writes its contents as a single line to the CSV file. For example, if you give it the list["one", "two", "three"], it will add the lineone,two,threeto the CSV file. -
writerows(): Takes a list of lists and writes each inner list on its own line in the CSV file. For example, if you give it the list of lists[["one", "two"], ["three", "four"]], it will write the following to the CSV file:
one,two
three,four
Here’s an example:
import csv
with open("some-other-file.csv", "w") as file:
writer = csv.writer(file)
# Write header row
writer.writerow([
"browser",
"vendor",
"year_appeared"
])
# Write data rows
data_rows = [
["Chrome", "Google", 2008],
["Safari", "Apple", 2003],
["Edge", "Microsoft", 2015]
]
writer.writerows(data_rows)
The code above produces a file named some-other-file.csv containing the following:
browser,vendor,year_appeared
Chrome,Google,2008
Safari,Apple,2003
Edge,Microsoft,2015
Advanced CSV File Reading with DictReader
While each row in a CSV file can be treated as a list of values, they can also be converted into dictionaries, where the field names are the keys. This approach makes the data easier to read and understand. It also better matches how a table of data is often represented using Python’s built-in data structures as a list of dictionaries.
To read a CSV file in such a way that each line is a dictionary, pass the file object to csv.DictReader(), which creates an iterator you can use in a for loop:
import csv
with open("some-other-file.csv", "r") as file:
reader = csv.DictReader(file)
for row in reader:
print(row)
The code above prints each row as a dictionary. For example, if the CSV file contains the following:
browser,vendor,year_appeared
Chrome,Google,2008
Safari,Apple,2003
Edge,Microsoft,2015
The code’s output would look like this:
{'browser': 'Chrome', 'vendor': 'Google', 'year_appeared': '2008'}
{'browser': 'Safari', 'vendor': 'Apple', 'year_appeared': '2003'}
{'browser': 'Edge', 'vendor': 'Microsoft', 'year_appeared': '2015'}
Note that using DictReader works only if the first line of the CSV file contains the column header names.
Advanced CSV File Writing with DictWriter
Just as reading a CSV file involves using a DictReader object, writing to a CSV file involves using a DictWriter object:
import csv
field_names = [
"browser",
"vendor",
"year_appeared"
]
data_rows = [
{
"browser": "Chrome",
"vendor": "Microsoft",
"year_appeared": 2008,
},
{
"browser": "Safari",
"vendor": "Apple",
"year_appeared": 2003,
},
{
"browser": "Edge",
"vendor": "Microsoft",
"year_appeared": 2015,
},
]
with open("yet-one-more-file.csv", 'w') as file:
writer = csv.DictWriter(file, fieldnames=field_names)
writer.writeheader()
for row in data_rows:
writer.writerow(row)
When using DictWriter, you need to define:
- The column header names as a list of strings. In the code above, this is
field_names. - The rows of data as a list of dictionaries, with each dictionary having the column header names as its keys. In the code above, this is
data_rows.
Once you’ve gathered this information, you:
- Pass the file object and the list of column header names to
csv.DictWriter()to get a writer object. - Write the row containing the column header names using the
writeheader()method. - Write the data rows using the
writerow()method.