The Broken Row: A Quoted Newline Shatters One Record Into Several Lines
A CSV file makes a silent bargain with its readers: one line, one record. The bargain holds only while no field value contains a newline, and real tex...
A CSV file makes a silent bargain with its readers: one line, one record. The bargain holds only while no field value contains a newline, and real text breaks it constantly. The format's answer is quoting: a field that contains a newline is wrapped in quotation marks, and the newline inside the quotes is data, not a boundary. Readers that honour the quotes see whole records. Readers that simply split the file into lines see fragments, and fragments do not join to anything. This article counts the shattering on a constructed CSV file and shows why a repair that glues one line back is still one line short.
What The Broken Row means
The Broken Row is a logical CSV record whose quoted field contains one or more embedded newlines, so that the record occupies several physical lines and any reader that splits on lines alone breaks it into a first-line fragment and orphan continuation lines. A row exhibits it when the record's note field joins multiple segments with real newlines inside quotation marks. The measurement held 300 logical records per source, which the second source rendered as a CSV text of 480 physical data lines (Source: batch 21 SD measurement results, group AL5).
> Census: 300 logical records rendered as 480 physical lines with 180 embedded newlines and 330 misaligned lines; naive line splitting recovered 150 of 300 records, merging one continuation recovered 270 of 300, and full CSV parsing recovered 300 of 300, with 0 false-merge pairs at the full-parse level (Source: batch 21 SD measurement results, group AL5)
The Broken Row differs from its nearest neighbour, The Split Byte, in the layer at which the cut falls. In The Split Byte, a byte budget severed a multi-byte character between its leading and continuation bytes, damaging the encoding of a single character inside a value. Here the encoding is flawless and every character survives; what is severed is the record itself, cut by physical line boundaries at points that fall inside a field. The breakpoint sits within the note's text, so the pieces are not even malformed lines — the first piece looks like a short record and the remaining pieces look like stray text. The Split Byte breaks characters and announces itself through invalid bytes; The Broken Row breaks records and disguises itself as extra rows, which is why its damage shows up as misalignment counts rather than decode errors.
Four hundred and eighty lines, three hundred records
The records were built from notes of one to three phrase segments. In the reference source the segments of each note were joined with single spaces; in the CSV source the same segments were joined with real newlines and written by a standard CSV writer, which quoted the field automatically whenever a newline was present (Source: batch 21 SD measurement results, group AL5). The segment profile was 150 records with one segment and no newline, 120 records with two segments and one embedded newline, and 30 records with three segments and two embedded newlines, giving 180 embedded newlines in total (Source: batch 21 SD measurement results, group AL5). Those 180 breaks inflate the file from 300 logical records to 480 physical data lines (Source: batch 21 SD measurement results, group AL5).
Splitting that text into physical lines and treating each line as a record recovered 150 of 300 records and failed on 150 (Source: batch 21 SD measurement results, group AL5). The survivors are the one-segment records, the only rows whose physical and logical shapes coincide. The remaining rows left 330 misaligned lines in the file: of the 480 physical lines, only 150 are complete record lines, and the rest are first-line fragments carrying a record's opening fields or orphan continuations carrying the tail of its note (Source: batch 21 SD measurement results, group AL5). The naive level also produced 4 false-merge pairs, because identical note-segment texts recur in different records and, once orphaned, collide with each other as naive keys (Source: batch 21 SD measurement results, group AL5).
Gluing one line back leaves the longest notes broken
A widespread heuristic repairs the split by watching the quotation marks: while the quote count on a line is unbalanced, glue the following line onto it — but only one following line. That repair recovered 270 of 300 records and failed on 30 (Source: batch 21 SD measurement results, group AL5). It rescued the entire two-segment block, all 120 records with a single embedded newline, and it stopped exactly one line short on every three-segment record, whose second embedded newline leaves the quote count still unbalanced after the single permitted merge. The heuristic's limit is not sloppiness; it is a design decision to bound the repair, made by tools that fear runaway merging more than incomplete recovery. The surviving failures are one whole construction block, the longest notes in the file.
A real parser, and a character count that agrees
The complete repair stopped counting lines altogether and parsed the entire text with a CSV reader that honours quoted newlines, treating the quotation marks as the authority on where records end (Source: batch 21 SD measurement results, group AL5). That parse recovered 300 of 300 records with 0 failures and 0 false-merge pairs (Source: batch 21 SD measurement results, group AL5).
The integrity check is a character census. Normalised, the notes of the reference source contain 8,120 characters in total, and the notes recovered by full parsing from the CSV source contain the same 8,120 characters (Source: batch 21 SD measurement results, group AL5). A recovery that dropped continuations, duplicated fragments, or trimmed text at the breaks would move that total; its stability across two very different readings of the data is the practical certificate that the parser reassembled the same notes the writer wrote.
Writing records designed to break
The file was composed to break in measured places. From seed 42, the generator created 300 records, each with an identifier, a name, and a note assembled from one to three phrase segments free of commas and quotation marks, so that newlines would be the only structural hazard in play (Source: batch 21 SD measurement results, group AL5). One source stored the notes flattened with spaces; the other let a standard CSV writer quote them honestly around their real newlines. Three readings were then applied to the identical text: physical line splitting, the single-continuation glue, and a full standards-respecting parse. The result is a deterministic synthetic measurement assembled from hand-written rules in the standard library; it audits no commercial product.
Questions, answered
Why does line splitting recover exactly half of the records? Because only the one-segment records occupy a single physical line. Line splitting recovered 150 of 300 records, precisely the no-newline block, and failed on all 150 records whose notes contain an embedded newline (Source: batch 21 SD measurement results, group AL5).
What are the 330 misaligned lines? They are the physical lines that are not complete records: first-line fragments and orphan continuations produced when 150 multi-segment records were cut at their embedded newlines, out of 480 physical lines in total (Source: batch 21 SD measurement results, group AL5).
Why does the single-continuation repair still lose 30 records? Because it merges at most one following line, and the three-segment records contain two embedded newlines each. The repair recovered 270 of 300 records and left exactly that 30-record block unrecovered (Source: batch 21 SD measurement results, group AL5).
Summary
The Broken Row lets a quoted newline turn one record into several physical lines, and a line-based reader recovers only the records that never contained a break: 150 of 300 by naive splitting, 270 of 300 when one continuation line may be glued back, and all 300 under a parser that honours the quotes, with 0 false merges (Source: batch 21 SD measurement results, group AL5). The note census of 8,120 characters from both readings certifies that the full parse restored the text intact (Source: batch 21 SD measurement results, group AL5). Against The Split Byte, which severs a character inside its encoding, the cut here severs the record inside its field, and the repair is a reader that lets quotation marks, not line breaks, decide where a record ends.
Sources
- Batch 21 SD measurement results, group AL5: 300 logical records per source; source B rendered as 480 physical data lines with 180 embedded newlines and 330 misaligned lines; note segments 150 with none, 120 with one newline, and 30 with two; naive line splitting recovered 150, failed 150, with 4 false-merge pairs; single-continuation repair recovered 270, failed 30; full CSV parsing recovered 300, failed 0, with 0 false-merge pairs; note characters 8,120 from both sources.
- Measurement script sd_batch21_measurements.py, group AL5: deterministic synthetic construction, seed 42, self-written parsing rules, pure standard library; not a commercial product measurement.