What is CSV?
Understand the CSV format and when to use it
Tabular data
Many real-world datasets are tabular — they have rows and columns, like a spreadsheet. CSV (Comma-Separated Values) is the most common format for storing tabular data in plain text.
CSV structure
A CSV file has one row per line. Values in each row are separated by commas. The first row is usually a header row that names the columns.
name,age,city
Alice,30,New York
Bob,25,London
Charlie,35,TokyoEach row has the same number of columns. This makes CSV easy to read, write, and share between programs.
Where CSV appears
- Data exports from spreadsheets (Excel, Google Sheets)
- Database dumps
- Machine learning datasets
- Financial reports and logs
Python's csv module
Python's built-in csv module handles the details of parsing CSV correctly — including values that contain commas, quotes, or newlines. Always use it instead of splitting strings manually.
| Class | Purpose |
|---|---|
csv.reader | Read rows as lists |
csv.DictReader | Read rows as dictionaries (column name → value) |
csv.writer | Write rows from lists |
csv.DictWriter | Write rows from dictionaries |
DictReader and DictWriter are especially useful — they keep column names tied to values, which makes your code easier to read.