Lesson ยท 15 min
Rows as dictionaries
The problem with positions
In the last lesson, the minutes of a trip were row[4]. You must remember that 4 means minutes. When someone adds a column to the file, 4 means something else. Your code then gives wrong answers, and it shows no error.
DictReader
csv.DictReader reads the header row for you. It gives each other row as a dictionary. The keys are the names of the columns.
import csv
with open("pets.csv", newline="", encoding="utf-8") as file:
pets = list(csv.DictReader(file))
print(pets[0]) # {'name': 'Biscuit', 'age': '4'}
print(pets[0]["name"]) # Biscuit
print(len(pets)) # 2
The list has no header row. Every item is a row of data. This shape is a list of dictionaries. It is the most common shape of data in Python, and you used it in the grade book project of the Python course.
Convert the numbers once
The values are still text. Convert each column of numbers one time, directly after you read the file. The rest of your code then works with real numbers.
for pet in pets:
pet["age"] = int(pet["age"])
Do not convert a column that is a label. A trip id such as 1017 looks like a number, but you never add two ids. Keep it as text.
Look at the data first
Before you calculate, look at what you have.
print(len(pets)) # the number of rows
print(pets[0]) # the first row: the keys and the kinds of values
Find the row with the largest value
Keep the best row so far in a variable. Compare each row with it.
oldest = pets[0]
for pet in pets:
if pet["age"] > oldest["age"]:
oldest = pet
A function that loads a file
Almost every program starts with a file that it reads. Put the steps in a function. The function takes the name of the file and returns the clean rows.
def load_pets(file_name):
with open(file_name, newline="", encoding="utf-8") as file:
rows = list(csv.DictReader(file))
for row in rows:
row["age"] = int(row["age"])
return rows
Your turn
The file trips.csv has 12 trips, and it has a new column, bike. Do the tasks in order.
Tasks
5 to do
Task 1, not checked yet. Import the
csvmodule. Readtrips.csvwithcsv.DictReaderinto a list namedtrips.Task 2, not checked yet. Change the value of
minutesin each trip to a whole number.Task 3, not checked yet. Make a variable
longest. It is the dictionary of the trip with the most minutes.Task 4, not checked yet. Make a variable
member_tripswith the number of trips that have the ridermember.Task 5, not checked yet. Write a function
load_trips(file_name). It reads a CSV file that has these columns, and it returns the list of dictionaries. It changesminutesto a whole number. All the other values stay text.
Task 1 of 5
Import the csv module. Read trips.csv with csv.DictReader into a list named trips.