.csv
Example
name,city,note
Smith, John,Boston,said "hi"name,city,note
"Smith, John",Boston,"said ""hi"""Quoting the field with a comma and doubling the inner quotes keeps each value in its own column.
Common mistakes
- Splitting lines on commas, which breaks on quoted values.
- Non-Latin text turning into garbage when the file opens in a spreadsheet.
- Leading zeros in codes like zip numbers disappearing after import.
Key points
- Fields with commas, quotes or line breaks are wrapped in double quotes, and inner quotes are doubled.
- CSV is parsed with a real CSV parser, not
line.split(','). - Exports escape cells starting with
=,+,-or@to prevent formula injection in spreadsheets. - Files are written in UTF-8, with a BOM when they must open correctly in spreadsheet apps.
Interview questions
CSV vs JSON: when should I use CSV?
Use CSV for flat tables that people open in spreadsheets. Use JSON for nested data or APIs.
What is CSV injection?
A cell starting with = or similar can run as a formula when the file opens in a spreadsheet. Prefix such values with a single quote when exporting.
Does CSV always use commas?
No. Some regions use semicolons because the comma is the decimal separator. Check the delimiter before parsing.