Skip to content
Start free

Profile November's file before you run anything

November's file just arrived. Count what is missing, repeated, impossible or out of place before the report touches it.

13 min · on a computer

Free course. Your Maestro, an AI, .

Before you touch a new file, bring back two facts from the first module: what does a single row in these monthly files stand for, and what explained the gap between October's report total and the number finance closed?

November's export just landed in data/sales_2026-11.csv. It is tempting to run report.py right away and see whether it prints a summary. Hold off on running it. A file that opens without error is not automatically clean. When Python's csv.DictReader reads a file, it hands you every cell as a plain string and checks none of your business rules or the table's .

If you run a report straight over an uninspected file, two things go wrong. Some defects crash the run on the first bad row and hide any other issues further down. Worse, other defects never crash at all: they quietly shift your regional totals by counting an order twice, subtracting a negative number, or sneaking in revenue from the wrong month.

Profiling means counting what is missing, repeated, unparseable, or out of range across the whole file before any analysis runs. In a monthly sales file, four checks cover the ground:

  1. Repeated keys against the table grain

    Because the table's grain is one row per order, each order_id must appear once. A repeated row does real damage: a loop that sums sales adds that order a second time and inflates the total.

  2. Blank cells in required columns

    An empty cell in a CSV arrives from csv.DictReader as "". If region is blank or holds only spaces, the order either drops out of the four regional totals or creates an unnamed group in history.csv.

  3. Unparseable text and negative amounts

    Even in a numeric column, csv.DictReader yields strings. Text that float() cannot read raises a ValueError and crashes the run, while a negative amount parses cleanly and quietly lowers a region's total.

  4. Dates outside the target month

    A valid date from October looks fine to a date parser, so a report would add its revenue into November without warning. Every order_date in November's file should start with "2026-11-".

In profile.py, the first check is already in place: Counter counts how many times each order_id appears and lists any that appear more than once. Write the other four in the same spirit: one list of offending order_id values per kind of problem. Convert amounts carefully: a check that crashes on the first bad value hides every problem after it. Ask your Maestro for a hint on testing whether text is a number without crashing.

Try it

Run profile.py first to see the row count and the repeated order_id. Then add the four remaining checks so the script prints the offending order_id values for each category, and let your output tell you what is inside data/sales_2026-11.csv before you inspect the raw file.

Your turn

Run profile.py. Add checks that list the order ids with a blank region, an amount that is not a number, a negative amount, and a date outside 2026-11. You're done when you can say which rows would change the report and how.

Once all five lines print, compare your output against the six rows in data/sales_2026-11.csv. Each of the five checks should flag one order_id. Together they show why profiling comes before running the report: one row would crash report.py on conversion, while four others would silently corrupt November's numbers. If any of your five lists came back empty or raised an error, share your code with your Maestro.

Check yourself: When csv.DictReader reads a monthly sales file and a loop sums float(r["amount"]) by r["region"], how does Python treat a repeated row and an amount written as "1,200"?