Morrow CreekData Services
← Morrow Creek Data Journal

When commas inside values turn a simple CSV into a parsing problem

Morrow Creek Data ServicesMorrow Creek Data Journal

A comma can be either data or structure.

In Northwest Supplies, Inc., the comma belongs to the company name. In a row such as company,city, the comma separates one field from another. A CSV reader has to distinguish between those two jobs. Code that splits every row at every comma cannot.

That distinction sounds small until a file reaches a spreadsheet, reporting process, or database. Then one comma in the wrong place can shift an amount under the wrong heading, separate part of an address from its record, or cause an import to fail. What began as a formatting mistake becomes a data-integrity problem.

CSV has rules, not just commas

Consider this row:

``text 101,"Morrow, Bell & Co.",Albany,"1,250.00" ``

It contains four fields: an ID, a company name, a city, and an amount formatted with a thousands separator.

A naïve split on commas produces six fragments instead. It reads the comma in the company name and the comma in the amount as if both marked new columns. Every value after the first unexpected comma is now at risk of landing in the wrong place.

The common CSV convention described in RFC 4180 uses double quotes to mark an enclosed field. Commas inside those quotes are part of the value, not delimiters. A field that contains a comma, a quotation mark, or a line break should be quoted.

Quotation marks need their own rule. A literal quotation mark within a quoted field is represented by two quotation marks:

``text 101,"The ""North"" Division",Albany ``

The recovered company name is The "North" Division.

These rules are why CSV should be treated as a small serialization format. It is plain text, but it is not safely parsed character by character without regard for context.

One line is not always one record

Commas are not the only complication. A quoted CSV field can contain a line break, which means one logical record may occupy several physical lines.

``text 42,"Send invoice to: Receiving Department 14 Market Street",Boston ``

Here, the line breaks belong to the instruction or address field. A process that assumes each text line is a separate record will turn one row into three.

This is a common source of confusion because a file can look reasonable when opened as text while still being structurally unsafe for a line-by-line reader. The receiving tool must understand quoted fields before it decides where a record ends.

The producer and the consumer have different jobs

The system creating the file should use a CSV-aware writer or its platform’s proper export function. Building rows by joining values with commas is fragile because every value must then be inspected and escaped correctly. That includes commas, quotes, line breaks, and blank values.

The system receiving the file should use a CSV-aware parser, not a general text split function. It should also know, or confirm, the file’s dialect: delimiter, quote character, quote-escaping convention, character encoding, header treatment, and newline behavior.

Those details vary. CSV has widely used conventions, but a .csv extension does not guarantee that every sender follows the same rules. Some files use semicolons rather than commas. Some exports use different encodings or quoting choices. A file handoff is more dependable when both parties state the format rather than assuming it.

A useful handoff checklist asks:

  • What separates fields?
  • What character encloses quoted fields?
  • How are quotation marks inside values represented?
  • Is the first record a header?
  • What encoding is used?
  • Can fields contain line breaks?
  • How many fields should a complete record contain?

Validate structure before trusting the values

For a known schema, one inexpensive early check is to parse the file and compare each record’s field count with the expected number of columns. Rows that do not match should be quarantined for review before loading, reporting, or analysis.

This check is useful, but limited. A row can have the expected number of fields and still contain a swapped value, an incorrect mapping, or a truncated description. Field counts test structure, not business meaning.

Export processes also benefit from a round-trip test. Create test records that include a comma in a company name, quotation marks in notes, a multiline address, a blank field, and a formatted value such as 1,250.00. Write the CSV, read it using the intended receiving tool, and compare the recovered fields with the originals.

That test checks the handoff that matters: not whether the file looks plausible, but whether its values survive being written and read.

Malformed CSV may not be repairable with confidence

Suppose a source sends this row:

``text Acme, Inc.,Boston ``

Without reliable quoting, a parser cannot know whether it represents two fields—Acme, Inc. and Boston—or three fields—Acme, Inc., and Boston. A guess may be informed by a known schema or source-system knowledge, but the characters alone do not settle the question.

That is why automatic cleanup should not be presented as a dependable cure for broken exports. The best correction is usually a corrected file or a documented rule from the source.

CSV remains useful for tabular exchange. Its failure mode is not that commas exist; it is that a workflow forgets their meaning depends on context. Get that context right at the handoff, and a comma stays where it belongs: in the data or between the fields, but never ambiguously both.

Have a dataset that needs to become usable?

Morrow Creek Data Services handles paid research, cleanup, verification, and structured delivery projects.

Start a data project