# .csv

> The `.csv` (Comma-Separated Values) is a simple file format for tabular data, where each row is a data record and columns are separated by commas.

## How do I handle commas and quotes in a CSV file?

The plain text format used to store data in a table-like structure. It's commonly used for exporting and importing data between systems, with each row representing a record and each column separated by commas.

- Topic: Document & Data Formats
- Type: format

## Example

Wrong:

```csv
name,city,note
Smith, John,Boston,said "hi"
```

Right:

```csv
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.

## Related cards

- [.json](https://wonderium.dev/engineering/json.md)
- [UTF-8](https://wonderium.dev/web/utf-8.md)
- [.split()](https://wonderium.dev/js/string-split.md)
- [.txt](https://wonderium.dev/engineering/txt.md)

---

- Level: [Engineering](https://wonderium.dev/engineering.md)
- HTML version: https://wonderium.dev/engineering/csv
- Last updated: 2026-11-25
