A CSV reader interprets delimiters and quoting so a comma inside a quoted field remains part of that field.
Python CSV imports: parse quoted fields before validating rows
Operation contract
The import reads a two-column header and one row whose note contains a comma. DictReader produces one amount and one note, then the application validates the amount as a positive integer. Splitting each line at commas would produce the wrong number of fields for this record.
Failure and ownership boundary
The program uses an in-memory text stream, not an uploaded file. A deployed import needs byte, row, field and retained-result bounds; accepting CSV does not provide them automatically. It also needs a duplicate-header and unexpected-column policy. Python input exercise: accept an explicit integer grammar and Python pathlib files: specify encoding and close the resource owner follow parsing.
Working program
import csv
import io
source = io.StringIO('amount_minor,note\n125,"review, accepted"\n', newline="")
reader = csv.DictReader(source)
if reader.fieldnames != ["amount_minor", "note"]:
raise ValueError("expected receipt header")
for row in reader:
if set(row) != {"amount_minor", "note"} or None in row.values():
raise ValueError("complete row required")
amount = int(row["amount_minor"])
if amount <= 0:
raise ValueError("positive amount required")
print(amount)
print(row["note"])Output
125
review, acceptedCosts and limits
This one-row fixture retains the entire short text. Streaming rows can avoid retaining all parsed rows, but a very large individual field still needs a limit.
Common Mistakes
- Do not split CSV at commas manually.
- Check header and row shape before using fields.
Connected lessons
Python input exercise: accept an explicit integer grammar, Python pathlib files: specify encoding and close the resource owner, Python JSON validation: reject duplicate members and non-integer amounts.
Apply this boundary
Python CSV ingestion: cap bytes, rows and fields before publication, Pandas nullable integers: missing amounts are not zero.
Check the next state boundary
Python JSON Lines: cap line bytes and validate the whole batch before returning it.
