CSV
CSV (Comma-Separated Values) is a simple, widely-supported file format for storing tabular data. Despite its simplicity, it remains one of the most common formats for data exchange between systems.
Format
name,email,age,active
Alice,alice@example.com,30,true
Bob,bob@example.com,25,true
Charlie,charlie@example.com,35,false
Key Characteristics
- Plain text: Human-readable, easy to inspect
- Universal support: Every spreadsheet and database can handle CSV
- No types: Everything is a string (parsing required)
- Delimiter variations: TSV (tabs), semicolons, pipes
Common Issues
| Issue | Example | Solution |
|---|---|---|
| Commas in values | "Smith, John" | Quote the field |
| Quotes in values | "He said ""hello""" | Escape with double quotes |
| Newlines in values | Multi-line text | Quote the field |
| Encoding | Non-ASCII characters | Use UTF-8, specify BOM |
| Large files | Memory issues | Stream processing |
Reading CSV in JavaScript
// Using PapaParse (browser and Node)
import Papa from 'papaparse';
Papa.parse(csvString, {
header: true,
dynamicTyping: true,
complete: (results) => {
console.log(results.data);
},
});
// Node.js with csv-parse
import { parse } from 'csv-parse/sync';
const records = parse(csvString, {
columns: true,
skip_empty_lines: true,
});
CSV vs Alternatives
| Format | Best For |
|---|---|
| CSV | Simple tabular data, spreadsheet import |
| JSON | Nested/hierarchical data, APIs |
| Parquet | Large datasets, analytics |
| Excel | Rich formatting, formulas |
What We Like
- Simplicity: Everyone understands CSV
- Compatibility: Works with every tool
- Streaming: Can process line by line
- Debugging: Easy to inspect and edit
What We Don't Like
- No schema: Type information lost
- No hierarchy: Flat structure only
- Ambiguous spec: Edge cases handled differently
- No compression: Larger than binary formats
- Encoding issues: UTF-8 not always assumed