Reading CSV with Embedded Commas
CSV readers treat commas inside quoted fields as data under the applicable dialect, so rows require a format-aware parser.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 1 min
In this article4 chapters
Trace a sample row
The line "North, Station",7 contains two fields. The first has an internal comma and the second contains the text seven. Use the delimiter and escape rules for the dialect you support.
Check output round trips
Writing those fields through a CSV writer and reading the result should recover both. Joining two strings with a comma alone loses the field boundaries. Round-trip tests can include an independent reference row to check compatibility.[1]
Walk the record according to quoting state
The row "green, large",12 contains two fields. The first comma is inside quotes and belongs to field one. The comma after the closing quote separates field two. A complete parser follows the format's dialect to distinguish separators from content. See CSV quotes and newlines.
Start quoted field
Read content green, large
Exit quoted field, then see separator
Field 2 reads 12
Test a comma that occurs next to an empty field
A record with a quoted comma and a trailing delimiter may have a final empty value. Keep apparent comma counts separate from parsed field counts. When exchanging data, agree on delimiter, quoting and header policy.