Python Arena
Step 1 of 3

The csv module

In part 6 you read CSV files by splitting each line yourself. That works until a value contains the separator itself, which happens more often than you'd think. The standard library's csv module reads CSV files properly. Here is the pets file from part 6 again, read with it:

The file pets.csv
Rex;dog;4
Misty;cat;11
Bubbles;fish;1
import csv

with open("pets.csv") as file:
    for row in csv.reader(file, delimiter=";"):
        print(row)
Output
['Rex', 'dog', '4']
['Misty', 'cat', '11']
['Bubbles', 'fish', '1']

csv.reader(file, delimiter=";") goes through the file one line at a time, and gives each line as a list of its values, already without the line break at the end, so there's nothing to strip. The delimiter is the character between the values. Leave it out and the reader assumes a comma: the C in CSV stands for comma, after all, although semicolons are common too.

The values are strings

As with split, every value is a string. Convert numbers yourself before calculating with them:

The file pets.csv
Rex;dog;4
Misty;cat;11
Bubbles;fish;1
import csv

total = 0
with open("pets.csv") as file:
    for row in csv.reader(file, delimiter=";"):
        name = row[0]
        age = int(row[2])
        total += age
        print(f"{name} is {age}")
print("Total age:", total)
Output
Rex is 4
Misty is 11
Bubbles is 1
Total age: 16

Why not split?

A value that contains the delimiter is put in double quotes. Spreadsheet programs do this when they save a CSV file. split doesn't know about quotes, but the csv module does:

The file books.csv
"Python, the basics",Ada Lovelace,2026
Short stories,"King, Stephen",2019
import csv

with open("books.csv") as file:
    for line in file:
        print(line.strip().split(","))

with open("books.csv") as file:
    for row in csv.reader(file):
        print(row)
Output
['"Python', ' the basics"', 'Ada Lovelace', '2026']
['Short stories', '"King', ' Stephen"', '2019']
['Python, the basics', 'Ada Lovelace', '2026']
['Short stories', 'King, Stephen', '2019']

Split cuts the titles and names in two and keeps the quotes. The reader gives three clean values per line.

csv.reader works on anything that gives lines of text, not only on an open file. A list of strings will do, which is handy for trying it out:

In the Python interpreter
>>> import csv
>>> lines = ['Ada;"Lovelace; Countess";1815']
>>> for row in csv.reader(lines, delimiter=";"):
...     print(row)
...
['Ada', 'Lovelace; Countess', '1815']

Your task

Challenge 1 of 3

The file scores.csv has a name and a score on each line, separated by a semicolon. Fill in the blanks so the program prints each line as in the example:

Ada;12
Linus;9
Ada scored 12
Linus scored 9

Challenge 2

The file weather.csv has a date and the day's highest temperature on each line, separated by a semicolon. Write a program that reads it with the csv module and prints the warmest day and its temperature. If several days share the highest temperature, print the first of them. For example, for this file:

2026-07-12;24
2026-07-13;29
2026-07-14;27

the program prints:

Warmest: 2026-07-13, 29 degrees

Challenge 3

The file products.csv has a product's name, its price and how many are in stock on each line, separated by commas. Some names have a comma in them, and those are in double quotes. Write a function stock_value(filename) that reads the file with the csv module and returns the total value of the stock: the sum of price times count over all the lines. For example, for this file the function returns 39.0:

"Pencils, box of 12",4.50,4
Eraser,0.75,8
"Notebook, A5",2.50,6