Snatch any course you want for just $15! Go to “Courses” and find a course ↓
Reading CSV files
6. Parsing a CSV into dictionaries


Great! Using list indices like row[0] or row[5] to get specific fields is quite straightforward, but the code is a bit unclean and difficult to read. Luckily, we can also parse a CSV file differently.

In the following code, we use the csv.DictReader() function to get a special object that contains one dictionary for each row (except the header) in our CSV. Recall that a dictionary contains key-value pairs. For each row, the corresponding dictionary maps the CSV field names to the cell contents in the row. Take a look:

import csv
with open('departments.csv') as csv_file:
  csv_reader = csv.DictReader(csv_file)
  line_count = 0
  for row in csv_reader:
    if line_count > 0 and float(row['budget']) > 350000.0:
      print('The', row['department'], 'department has a budget of', row['budget'], 'USD.')
    line_count += 1

The code above prints the same output as before, but it is much easier to read. Instead of csv.reader(csv_file), we used csv.DictReader(csv_file). The DictReader only works if the first line of the CSV contains a header. The DictReader uses the field names from the header to create a dictionary for every line/row of the CSV. With this, we can use field names (row['department'], row['budget'], etc.) instead of field indices (row[0], row[2], etc.).


Modify the template code so that it uses a DictReader instead of the default reader. For this, you'll need the following columns: 'name', 'country', and 'age'.

Stuck? Here's a hint!

Take a look at the sample code in the explanation. Use csv.DictReader() instead of csv.reader().