By Paradigm Study · Updated September 6, 2026
Python CSV Practice: Check the Code AI Gives You
Check an AI-generated CSV cleaner by defining the input and expected output before reading its proposed code. This exercise uses six synthetic rows, including a blank quantity, invalid text and an unexpected region. You will decide which records are accepted, which are rejected and why. A script that runs without crashing is not necessarily applying the right rules.
Start with a clear contract
Download the input CSV, the reference cleaner and the executable checks. Put the three files in one folder. Run python3 test_clean_orders.py to check the reference implementation, or replace its clean function with your own solution and run the same checks.
The columns are order_id, region and units. Strip surrounding whitespace. Accept regions North, South and West after converting to lowercase. Units must be a nonnegative integer. A blank quantity means unknown, not zero; reject it and record the reason. This policy is a teaching choice, not a universal rule for business data.
Predict the result
| Order | Raw region | Raw units | Expected result |
|---|---|---|---|
| 101 | North | 3 | Accept as north, 3 |
| 102 | south | blank | Reject: missing units |
| 103 | WEST | 2 | Accept as west, 2 |
| 104 | north | many | Reject: invalid units |
| 105 | west | 0 | Accept as west, 0 |
| 106 | east | 4 | Reject: unknown region |
The accepted units total is 5 across three accepted orders. Zero is valid in this contract. Unknown is not equivalent to zero, and dropping a row without explanation would hide the distinction. Before running the code, write the accepted order IDs on paper and identify the rejected records.
Read the important operations
Python's CSV documentation provides DictReader for named-column records. The reference uses it with a file opened using newline="" and UTF-8 encoding. It normalizes the region, checks the allowed set, and converts the units with int. It catches the expected ValueError from invalid integer text and records a rejection.
A broad except Exception: pass would be a bad repair: it could silently hide a programming error as well as malformed input. Likewise, replacing every blank with zero would change the meaning of the data. Ask an AI assistant to explain those choices before accepting its implementation.
Test a change the prompt did not mention
Add an order with units -1. It should be rejected as invalid units. Add one with region north and units 4; it should be accepted after trimming. Add one with units 2.5; the integer contract rejects it. If fractional units are valid in your real domain, change the contract and tests together rather than silently rounding.
The download includes checks for these cases, the supplied fixture and empty input. It does not establish that the cleaner handles every possible CSV dialect, duplicate identifier or malformed row. Those are separate requirements to decide when the task grows.
Explain the cleaned output
Run python3 clean_orders.py to print accepted rows and rejection reasons as JSON. Reconcile accepted plus rejected rows with the input count. Do not report the accepted total as the total for all orders: three records still need attention.
If you can run the script but cannot explain a rejection, use the debugging-with-hints exercise. Then move to SQL join checks to see how correct-looking output can still double-count data.
Sources and further practice
Python documentation: csv supports the reference principle used here. The exercise, example data and review routine on this page are original Paradigm Study teaching examples.
For a broader workflow, see coding learners. Bring your attempt and the step that confused you into Paradigm Study for a lesson or focused practice. Start a learning notebook.