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, JoDictReader 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.
A cleaning routine
- Trim whitespace before anything else.
- Convert types, deciding what to do with failures.
- Choose a rule for duplicates, and normalize before comparing.
- Keep the rules in one function, so you can read and rerun them.
In a notebook, keep the original data untouched and clean into a new variable. Then you can compare the two and rerun from the top.
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.
The explanation opens here once every check passes.