What is CSV?
4 min read · Data formats
The short answer
CSV (Comma-Separated Values) is a plain text format for tabular data — rows and columns, like a spreadsheet. Each line is a row, and each value within a row is separated by a comma.
name,email,age,isPremium Ana García,ana@example.com,28,true Luis Martínez,luis@example.com,34,false Sara Kim,sara@example.com,25,true
The first row is typically a header row with column names. Every other row is a data record.
Why CSV exists
CSV has been used since the 1970s. Its advantages are simple: it is human-readable, universally supported, and extremely compact. Almost every database, spreadsheet app, and data tool can import and export CSV.
Common CSV use cases:
- Exporting data from a database for analysis in Excel or Google Sheets
- Importing bulk records (users, products, orders) into an application
- Sharing datasets between teams or tools
- Data pipeline input/output files
CSV edge cases and gotchas
CSV looks simple but has several tricky rules:
Values with commas must be quoted
"García, Ana",ana@example.com
If a value contains a comma, wrap the entire value in double quotes.
Values with newlines must be quoted
"Line one Line two",other-value
Multi-line values are allowed but must be quoted.
Quotes inside quoted values must be escaped
"He said ""hello""",other-value
In CSV, a double quote inside a quoted field is escaped as two double quotes.
The separator is not always a comma
Some files use semicolons (;) or tabs (\t) as separators — especially Excel exports in European locales where commas are used as decimal separators. Tab-separated files are called TSV.
Reading and writing CSV in code
In JavaScript / Node.js — never parse CSV manually. Use a library:
// npm install papaparse
import Papa from 'papaparse';
// Parse CSV string
const result = Papa.parse(csvString, { header: true });
console.log(result.data);
// [{ name: 'Ana', email: 'ana@example.com', age: '28' }, ...]
// Generate CSV from array of objects
const csv = Papa.unparse(result.data);In Python — the standard library has csv built in:
import csv
# Reading
with open('data.csv', newline='') as f:
reader = csv.DictReader(f)
for row in reader:
print(row['name'], row['email'])
# Writing
with open('output.csv', 'w', newline='') as f:
writer = csv.DictWriter(f, fieldnames=['name', 'email'])
writer.writeheader()
writer.writerow({'name': 'Ana', 'email': 'ana@example.com'})CSV vs JSON
| CSV | JSON | |
|---|---|---|
| Structure | Flat rows only | Nested objects and arrays |
| Data types | All strings | Strings, numbers, booleans, null |
| File size | Smaller (no keys repeated) | Larger (keys in every row) |
| Human readable | Easy for tabular data | Better for complex structures |
| Spreadsheet support | Native | Needs conversion |
| Best for | Bulk data export/import | APIs and config |
Convert between CSV and JSON
Paste CSV data and convert it to JSON, or convert a JSON array back to a CSV file.