Python CSV · Exercises
Practical Python

Python CSV Exercises

4 exercises that build on the Python CSV Files lesson, from easy to hard. Each one is checked in your browser when you press Check. Stuck? Use the hint before peeking at the solution.

What this lesson covers: A CSV file stores a table as plain text — one row per line, values separated by commas — and Python's csv module reads and writes it correctly, including tricky cases such as commas inside quoted values. By the end you can read CSV data with csv.reader and csv.DictReader, convert text columns to numbers, calculate totals, and write CSV files with csv.writer and csv.DictWriter. You will practise: Total a column; Predict the parsed rows; Fix the top-score finder; Write a summary CSV.

0 / 4 completed

Python CSV practice exercises

1. Total a column

Write code Easy

Finish column_total(csv_text, column) so it returns the sum of all values in the named column, converted with float(). The reader is already set up for you.

Your code
import csv
import io

def column_total(csv_text, column):
    reader = csv.DictReader(io.StringIO(csv_text))
    total = 0
    # loop over the rows and add float(row[column]) to total
    return total

orders = "item,qty,price\nTea,2,4.50\nCake,1,3.25\n"
print(column_total(orders, "price"))

2. Predict the parsed rows

Predict the output Medium

The second line has a quoted value that contains a comma. Type exactly what the program prints.

Read this code
import csv
import io

text = 'name,tags\nAsha,"python,sql"\nBen,excel\n'
rows = list(csv.reader(io.StringIO(text)))
print(len(rows))
print(rows[1])
print(rows[2][1])

3. Fix the top-score finder

Fix the bug Medium

This code should find the student with the highest score and print Chen 91, but it crashes with a ValueError. Fix it so best_name and best_score end up correct.

Your code
import csv
import io

scores_csv = "student,score\nAsha,82\nBen,67\nChen,91\n"
reader = csv.reader(io.StringIO(scores_csv))

best_name, best_score = "", 0
for row in reader:
    score = int(row[1])
    if score > best_score:
        best_name, best_score = row[0], score
print(best_name, best_score)

4. Write a summary CSV

Write code Hard

Write summarize(in_file, out_file):

  1. Read in_file (columns region,amount) and add up amount for each region.
  2. Write out_file as CSV with the header region,total, then one row per region in alphabetical order, with the total formatted to two decimals (for example 150.75 or 80.00).
Your code
import csv

def summarize(in_file, out_file):
    # 1. read in_file with csv.DictReader and total "amount" per "region"
    # 2. write out_file with csv.writer: header, then sorted rows
    pass

with open("sales.csv", "w", newline="") as f:
    f.write("region,amount\nNorth,120.50\nSouth,80\nNorth,30.25\nEast,55\n")

summarize("sales.csv", "summary.csv")
with open("summary.csv", newline="") as f:
    for row in csv.reader(f):
        print(row)

Next lessonPython Regular Expressions (regex)

More Python CSV Files examples · All Python exercises