.csv

Term side of the Card.
Topic: Document & Data Formats
Unit title: .csv
Categories: Format
Explanation side of the Card

Explanation: 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.

Example

Wrongcsv
name,city,note
Smith, John,Boston,said "hi"
Rightcsv
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.

Analytics cookiesWe'd like to use analytics cookies to see which pages are useful. They're set only if you accept. Privacy Policy