Free lab Real Python 3. Zero installs. Your code stays in this browser. Open the playground

Crunching CSV Data

16 min 35 XP
Study loop Read Predict Run Tweak Prove Review
Lesson 3 of 5 · View course roadmap
Step 1

Learn the idea

CSV (comma-separated values) is the universal data format — every spreadsheet exports it. Python's csv module handles the tricky parts (quoted fields, commas inside values):

  • csv.reader(f) — rows as lists
  • csv.DictReader(f) — rows as dicts keyed by the header row ← usually what you want
  • csv.DictWriter(f, fieldnames=[...]) — write dicts back out

The analysis pattern: read rows → convert types (CSV gives you strings, even for numbers!) → filter → aggregate. Forgetting int(row["price"]) is the #1 CSV bug.

Where you'll use this

Every ERP, bank, ad platform and spreadsheet exports CSV — it's the lingua franca between systems that don't share an API. This read → convert → aggregate pattern is the seed of every data pipeline (pandas industrialises exactly it).

Common mistakes

  • The #1 CSV bug: forgetting values are strings — "5" * "2" errors and "10" < "9" is True. Convert types immediately.
  • Naive parsing with .split(",") — real CSVs contain quoted fields with commas inside; the csv module exists because of them.
  • Writing CSVs without newline="" in open() on Windows — you get blank rows between every record.

Pro tip

When a CSV outgrows the csv module (millions of rows, joins, pivots), the upgrade path is pandas: pd.read_csv(f) gives you a DataFrame — and your DictReader mental model transfers directly.

Step 2

Try it yourself

Blank · autosaved

The lesson example is loaded and ready — press Run, then change something and run it again. Breaking it is part of learning. Want a clean slate? Tap “New blank”.

PYexample.py
+ Enter to run
Output appears here…
Step 3

Pass the challenge +35 XP

Blank · autosaved

Parse the CSV in the starter with DictReader and print: the number of orders, the total revenue (qty × price summed), and the name of the product with the biggest single order value (qty × price).

Target output
3
3200
laptop
PYchallenge.py
Run your code to check it…
Step 4

Check your understanding

1. What type are values read from a CSV file?
2. DictReader keys come from…
Last step

Your notes (saved on this device)

Tip: use and to move between lessons, K to search everything.

Your next ten minutes

Write Python that does something useful.

Start free. No install, no card, no passive video marathon.

Start learning free → Explore the path