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
| Rule | Example |
|---|---|
| Fields separated by comma | a,b,c |
| Rows separated by CRLF | a,b\r\nc,d |
| Quote fields with commas/quotes/newlines | "a,b",c |
| Escape quotes by doubling | "say ""hi""" |
| Optional header row | name,age |
Quoting and escaping
code
name,note
"Doe, John","He said ""hello"""
"Multi
line",value
Table
| Input | Encoded |
|---|---|
Doe, John | "Doe, John" |
He said "hi" | "He said ""hi""" |
| Embedded newline | Wrap the whole field in quotes. |
Common delimiters
Table
| Delimiter | Used by |
|---|---|
, | Default (comma-separated). |
; | European locales (Excel). |
\t | TSV (tab-separated). |
| | Pipe-delimited exports. |
Parsing pitfalls
Table
| Pitfall | Fix |
|---|---|
| Delimiter inside quoted field | Use a real CSV parser, not split(","). |
| BOM at file start | Strip \uFEFF. |
| Mixed line endings | Normalize to \n. |
| Empty trailing lines | Trim before parsing. |
| Inconsistent columns | Validate 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;
});