DevTools Logo

CSV Data Cheat Sheet

CSV format rules, delimiters, quoting, escaping, parsing pitfalls, and common transformations.

Data Formats
csv
data
parsing

CSV stores tabular data as plain text with rows separated by newlines and fields separated by a delimiter (usually a comma). The tricky parts are quoting, escaping, and embedded delimiters.

Format rules (RFC 4180)

Table
RuleExample
Fields separated by commaa,b,c
Rows separated by CRLFa,b\r\nc,d
Quote fields with commas/quotes/newlines"a,b",c
Escape quotes by doubling"say ""hi"""
Optional header rowname,age

Quoting and escaping

code
name,note
"Doe, John","He said ""hello"""
"Multi
line",value
Table
InputEncoded
Doe, John"Doe, John"
He said "hi""He said ""hi"""
Embedded newlineWrap the whole field in quotes.

Common delimiters

Table
DelimiterUsed by
,Default (comma-separated).
;European locales (Excel).
\tTSV (tab-separated).
|Pipe-delimited exports.

Parsing pitfalls

Table
PitfallFix
Delimiter inside quoted fieldUse a real CSV parser, not split(",").
BOM at file startStrip \uFEFF.
Mixed line endingsNormalize to \n.
Empty trailing linesTrim before parsing.
Inconsistent columnsValidate row lengths.

Parsing in JavaScript

js
const rows = csvText.trim().split(/\r?\n/).map((row) => {
  const cols = [];
  let cur = "", inQuotes = false;
  for (let i = 0; i < row.length; i++) {
    const c = row[i];
    if (c === '"') {
      if (inQuotes && row[i + 1] === '"') { cur += '"'; i++; }
      else inQuotes = !inQuotes;
    } else if (c === "," && !inQuotes) { cols.push(cur); cur = ""; }
    else cur += c;
  }
  cols.push(cur);
  return cols;
});

References

Related tools