PythiloLesson 3 of 4 · progress kept in this browser

CSV and cleaning data

Real CSV files contain stray spaces, blanks and duplicates. Use the csv module rather than split(","), because quoted fields may contain commas.

import csv, io

text = 'name,city\n"Doe, Jo",Oslo\n'
reader = csv.DictReader(io.StringIO(text))
for row in reader:
    print(row["name"])  # Doe, Jo

DictReader uses the header row as keys. Every value is a string, and a row with too few fields gets None for the missing ones. For a file, pass an open file with newline="" instead of StringIO.

Your task

Write clean_rows(text) reading CSV text with name and age columns and returning a list of {"name": str, "age": int}. Trim both fields, drop rows with an empty name or an age that is not a whole number, and drop later duplicates of a name, ignoring case. Write average_age(rows) returning the mean, or None if there are no rows.