1 of 4
Split a delimited line the way csv does, and see what a naive split costs before it costs you.
The header names six columns. A parser that splits this line on every comma produces how many fields?
order_id,customer,placed_at,quantity,total,status 1003,lin@example.com,2026-03-02T09:14:00,3,"1,499.00",pending
Records and formats· 2 of 3
Reading a messy file
Parse a delimited file by hand once, so you know exactly what the csv module is doing for you and exactly which of your assumptions it does not share.
25 minute read
Production systems rely on standard libraries like Python's csv module rather than custom parsers. Implementing a parser manually once exposes the specific failure modes that occur in production pipelines when assumptions about input formatting break down.
The naive parser and boundary cases
Splitting lines on delimiters is a common initial approach:
def parse(text):
lines = text.strip().split("\n")
header = lines[0].split(",")
return [dict(zip(header, line.split(","))) for line in lines[1:]]
This implementation functions on basic inputs, which makes its edge cases difficult to detect. Consider this extract row:
1003,lin@example.com,2026-03-02T09:14:00,3,"1,499.00",pending
The total value 1,499.00 contains a comma and is wrapped in double quotes. The naive parser splits this into "1 and 499.00", yielding seven fields instead of the six defined in the header. Calling zip truncates the trailing element without raising an exception. The total value is recorded as "1 and the status column is dropped.
This illustrates a common failure mode: corrupt data often produces structurally valid records rather than raising runtime errors.
Quoting rules in delimited files
Standard delimited parsing depends on three core rules:
- Fields containing delimiters or special characters may be enclosed in double quotes.
- Inside a quoted field, delimiter characters are treated as literal text.
- Inside a quoted field, two consecutive double quotes represent an escaped literal double quote.
A reliable parser operates as a state machine: iterating over characters sequentially, maintaining state on whether the pointer is within quoted bounds, and terminating a field on delimiter characters only when outside quotes.
Embedded newlines within quoted fields are also valid in CSV specifications. Pre-splitting raw input on \n fails on multiline fields, which is why standard library parsers process input as continuous streams.
Handling rows with mismatched field counts
Consider this record:
1007,ada@example.com,2026-03-05,1,9.99
The record contains five fields against a six-column header due to upstream export truncation or missing trailing values.
Parsing engines must handle mismatched lengths explicitly. Padding with None records that the final value was missing. Dropping the record removes evidence of the transaction entirely. Standard parsers such as csv.DictReader pad missing values with None. Retaining incomplete records ensures downstream systems have visibility into dropped or malformed data.
Preserving an incomplete record allows errors to be audited, whereas silent deletion makes failures untraceable.
What the exercise asks
Implement four functions:
- A field splitter that handles quoted strings and escaped quotes.
- A parser that transforms raw delimited text into a list of dictionaries.
- A validation helper that identifies rows where field counts deviate from the header.
- An aggregation helper to inspect the parsed results.
Subsequent modules use Python's built-in csv module for file ingestion.
Reading a messy file
Parse a delimited file by hand once, so you know exactly what the csv module is doing for you and exactly which of your assumptions it does not share.
25 minute read
Implement your solution in the editor on the right or work in your local IDE. All unit tests run against your code in real-time.
Exercise
read-csv
26 tests, graded on your machine in about a second. Stuck for 25 minutes? The desktop app will offer you the reference solution.