Complete worked example
python
import csv
import io
def inspect_example(text):
reader = csv.DictReader(io.StringIO(text, newline=''))
if reader.fieldnames != ['id', 'description', 'amount_cents']:
raise ValueError('unexpected header')
rows = []
for row in reader:
if None in row or any(value is None for value in row.values()):
raise ValueError('wrong field count')
rows.append([row['id'], row['description'], int(row['amount_cents'])])
return rows- Reasoning
- DictReader associates each parsed field with its header after resolving quoting. The worked program checks the header, validates missing or extra fields, converts cents, and returns canonical row lists. Output order follows the source records rather than an unrelated alphabetical sort.