Demo
In this demo, you’ll read and write CSV files. Just as you did in the demo for text files, you’ll first write some code to write a CSV file and then write code to read it.
Writing To a CSV File From a Collection of Lists
Write a new CSV file. Open the working-with-files-starter.ipynb notebook and scroll to the Working with CSV Files section.
Enter the following into a code cell and run it:
import csv
with open('operating-systems.csv', 'w') as file:
writer = csv.writer(file)
# Write header row
writer.writerow(['operating_system', 'creator', 'year'])
# Write data rows
data_rows = [
["MS-DOS", "Microsoft", 1981],
["macOS", "Apple", 1984],
["Windows", "Microsoft", 1985],
["Linux", "Linus Torvalds", 1991],
]
writer.writerows(data_rows)
The code above opens the file operating-systems.csv for writing. It creates the file if it doesn’t already exist. The code then creates a file object to operate on that file and uses that file object to create a CSV writer object.
Using the writer object, the code writes the first row containing the column header names using the writerow() method. writerow() takes a single list and writes a corresponding single line to the CSV file.
Writing data rows also uses the writerows() method. Here, writerows() takes a list of lists, writing a corresponding line for each inner list to the CSV file.
Open operating-systems.csv with a text editor by right-clicking on the file in the JupyterLab left navigation pane. Then click Open with| Editor. You’ll see this:
operating_system,creator,year
MS-DOS,Microsoft,1981
macOS,Apple,1984
Windows,Microsoft,1985
Linux,Linus Torvalds,1991
If you open operating-systems.csv by double-clicking it in JupyterLab, you’ll see the same data but displayed in a spreadsheet-like table.
Reading a CSV File as a Collection of Lists
Now, write some code to read the CSV file that the previous code wrote. Run the following in a new code cell:
with open("operating-systems.csv", "r") as file:
reader = csv.reader(file)
for row in reader:
print(row)
This code opens operating-systems.csv for reading, creates a file object to operate on that file, and uses that file object to create a CSV reader object.
The reader object is an iterator, so you can use a for loop to read the CSV file line by line. The reader converts each line into a corresponding Python list.
Here’s the output of the code above — one list for each line of the CSV file:
['operating_system', 'creator', 'year']
['MS-DOS', 'Microsoft', '1981']
['macOS', 'Apple', '1984']
['Windows', 'Microsoft', '1985']
['Linux', 'Linus Torvalds', '1991']
Reading a CSV File as a Collection of Dictionaries
Use DictReader to read operating-systems.csv as a collection of dictionaries. This is possible because the file’s first line contains the column headers’ names.
Enter the code below into a new code cell and run it:
with open("operating-systems.csv", "r") as file:
reader = csv.DictReader(file)
for row in reader:
print(row)
This code is similar to the code that reads a CSV file as a collection of lists. The only difference is that instead of passing the file object to csv.reader(), the file object is passed to csv.DictReader().
Here’s the output from the code above:
{'operating_system': 'MS-DOS', 'creator': 'Microsoft', 'year': '1981'}
{'operating_system': 'macOS', 'creator': 'Apple', 'year': '1984'}
{'operating_system': 'Windows', 'creator': 'Microsoft', 'year': '1985'}
{'operating_system': 'Linux', 'creator': 'Linus Torvalds', 'year': '1991'}
Writing To a CSV File From a List of Dictionaries
Finally, write the CSV version of the “programming languages” file you wrote for the text files demo. This will use DictReader’s counterpart, DictWriter, to write the file and the programming_languages list as the data source.
To use DictWriter, you need to define two data structures:
- A list containing the names of the column headers.
-
A list of dictionaries, where each dictionary’s keys are the column headers. In this case, this list is
programming_languages.
Run the following in a new code cell:
field_names = [
"name",
"creator",
"year_appeared",
"site_url",
"active",
]
with open("programming languages.csv", 'w') as file:
writer = csv.DictWriter(file, fieldnames=field_names)
writer.writeheader()
for row in programming_languages:
writer.writerow(row)
Note that DictWriter needs more than just the file object; it also requires the list of column header names.
Once the writer object has been created, the writeheader() method is used to write the column header names to the CSV file, and the writerow() method is used to write the data rows.
You can confirm that the code above works by running this in a new code cell:
with open("programming languages.csv", "r") as file:
reader = csv.DictReader(file)
for row in reader:
print(row)
Here’s the first line of output from the above code:
{'name': 'Python', 'creator': 'Guido van Rossum', 'year_appeared': '1991', 'site_url': 'https://www.python.org/', 'active': 'True'}
...