Using Python CSV #3 — DictReader
Python’s csv
module allows you to work with csv files (e.g., Excel spreadsheets). In previous articles (#1 and #2), I looked at two functions of thecsv
module: csv.reader
and csv.writer
. In essence, these two functions allow you to read and write csv files using lists. Two other functions worth examining are csv.DictReader
and csv.DictWriter
. These functions have the same purpose but involve dictionaries. In this article, we’ll look at csv.DictReader
and in the next article csv.DictWriter
.
Creating a CSV File
First, let’s create a csv. If you read my Using Python CSV #2 — Basic Writing, then you know that we can create a csv file with csv.writer
. Let’s create a hypothetical csv that contains the name, age, and account size of various clients.
headers = ['name', 'age', 'account']
names = ['Tek', 'Sara', 'John']
ages = [18, 30, 65]
account = [504, 240890, 800489]
combined = list(zip(names, ages, account))
with open('retirement.csv', 'w', newline="") as outfile:
writer = csv.writer(outfile)
writer.writerow(headers)
writer.writerows(combined)
Here is the result:
name,age,account
Tek,18,504
Sara,30,240890
John,65,800489
Examining the DictReader Object
The csv.DictReader
is a function returns a DictReader object given a csv file. If we iterate over it, we see it contains dictionaries whose elements (keys and values) are strings. Let’s print each item in the DictReader object by writing the following:
with open('retirement.csv', 'r', newline='') as infile:
reader = csv.DictReader(infile)
for row in reader:
print(type(row))
Result:
<class 'dict'>
<class 'dict'>
<class 'dict'>
Since it is a dictionary, values are mapped to keys. In spreadsheet language, data in the rows are mapped to fieldnames. In even simpler language, we have items and these items have names that we can use to refer to those items.
Fieldnames are either explicitly specified or are the first row of the csv. Our fieldnames should be the first row (the header): ‘name’, ‘age’, and ‘account’. These should map to the name, age, and account size of each person. Let’s check by printing out each dictionary. We can do this by printing each item in the DictReader
object:
with open('retirement.csv', 'r', newline='') as infile:
reader = csv.DictReader(infile)
for row in reader:
print(row)
Result:
{'name': 'Tek', 'age': '18', 'account': '504'}
{'name': 'Sara', 'age': '30', 'account': '240890'}
{'name': 'John', 'age': '65', 'account': '800489'}
Our output are some dictionaries with both the key and the value being strings. If only we want to know the fieldnames of the DictReader, we can do this using the fieldnames
parameter. Let’s check the fieldnames of our csv file using this parameter:
with open('retirement.csv', 'r', newline='') as infile:
reader = csv.DictReader(infile)
print(reader.fieldnames) #prints the fieldnames
Result:
['name', 'age', 'account']
Why use csv.DictReader instead of csv.Reader?
One advantage of DictReader
over the regular csv.reader
is usuability. We can access data using the fieldnames (keys) rather than the index of the row. Suppose I want to know the mean age of clients in our list. If we have 50 columns of data, rather than tracking down the row’s index, wecan pull up the data more quickly using the ‘age’ fieldname.
with open('retirement.csv', 'r', newline='') as infile:
reader = csv.DictReader(infile)
for row in reader:
print(row['age']) # 'age' fieldname!
Result:
18
30
65
Let’s consider another example illustrating the usability of DictReader
. Suppose you just lost your client information and a scammer (me) has your csv file. I do not have time to scam everyone on your list so I’m only going to focus on the “big fish”. I’ll go ahead and target those individuals with over $500,000 in their accounts and are over 60 years old. Using fieldnames (keys) makes composing an eloquent scam email so much easier.
with open('retirement.csv', 'r', newline='') as infile:
reader = csv.DictReader(infile)
for row in reader:
if int(row['account']) > 500000 and int(row['age'])>60:
print(f"Dear {row['name']}."\
f"\nWe noticed that you have ${row['account']} in your account."
f"\nAre you interested in our new scammy product?"
f"\nSincerly.\nMr. Scammer")
Result:
Dear John.
We noticed that you have $800489 in your account.
Are you interested in our new scammy product?
Sincerly.
Mr. Scammer
Resources and Earlier Articles in Series
- CSV File Reading and Writing — Python CSV Documentation
- Using Python CSV #1 — Basic Reading by David W. Agler
- Using Python CSV #2 — Basic Writing by David W. Agler