Usage of csv.DictReader

CSV, or "comma-separated values", is a common file format for data. The csv module helps you to elegantly process data stored within a CSV file. Also see the csv documentation.

This guide uses the following example file, people.csv.

id,name,age,height,weight
1,Alice,20,62,120.6
2,Freddie,21,74,190.6
3,Bob,17,68,120.0

Your Python code must import the csv library.

import csv

Open the file by calling open as we usually do on a file and then call csv.DictReader.

people_csv = open("people.csv")
input_file = csv.DictReader(people_csv)

You may then iterate over the rows of the csv file by iterating over input_file. (Similarly to other files, you need to re-open the file if you want to iterate a second time.)

for row in input_file:
    print(row)

Remember how when you iterate over a normal file, each iteration of the loop produces a single string that represents the contents of that line. Sometimes we then use split to convert that into a list.

When you iterate over a CSV file using csv.DictReader, each iteration of the loop produces a dictionary mapping keys that are strings to values that are strings. They keys are the names of the columns (from the first row of the file, which is skipped over), and the values are the data from the row being read. For example, the above loop prints the following. Note how this relates to the contents of the file people.csv shown at the top of this page. Also note that what is printed out are dictionaries, which have no ordering.

{'age': '20', 'height': '62', 'id': '1', 'weight': '120.6', 'name': 'Alice'}
{'age': '21', 'height': '74', 'id': '2', 'weight': '190.6', 'name': 'Freddie'}
{'age': '17', 'height': '68', 'id': '3', 'weight': '120.0', 'name': 'Bob'}

Note: Technically, what is now returned is an OrderedDict that remembers the order that mappings were added - you do not need to worry about this, you can treat what is returned like a regular dictionary.

Finally, here is a complete example usage of csv.DictReader using people.csv. This example finds the oldest person within the file and that person's age.

import csv

people_csv = open("people.csv")
input_file = csv.DictReader(people_csv)

max_age = None
oldest_person = None
for row in input_file:
    age = int(row["age"])
    if max_age is None or max_age < age:
        max_age = age
        oldest_person = row["name"]
people_csv.close()

if max_age is not None:
    print("The oldest person is", oldest_person,
          "who is", max_age, "years old.")
else:
    print("The file does not contain any people.")

And the output from this program:

The oldest person is Freddie, who is 21 years old.