LambdaLabTM
Computer Science · Class 12 · CSV Files
CSV fileswhy⏱️ 8 min read

Why a CSV File

A lot of the data you meet is a table — marks, attendance, a price list, a bank statement. A CSV file is one of the most popular ways of keeping a table like that in a plain text file, and it is popular for one reason: almost every program on earth can read one.

The lesson these programs practiseIntroduction to Files — where CSV was first named

1Tabular data

Tabular data is data arranged in rows and columns. Each row is one record — one student, one item, one payment — and every record has the same columns as every other one.

RollNameMarks
1Ravi78
2Meera91
3Amit65

You have seen this shape drawn as a grid all your life — in Excel, on a report card, on the board. But a grid is a picture of the data. A file cannot store lines and boxes. It stores characters, one after another, so a table has to be written down some other way.

2The same table, written down

A CSV file writes it like this: one line for each row, and inside a line, a comma between the values.

how you picture it
RollNameMarks
1Ravi78
2Meera91
3Amit65
how the file stores it
Roll,Name,Marks
1,Ravi,78
2,Meera,91
3,Amit,65

Four lines. The highlighted commas are the only thing marking where one column ends and the next begins.

That is the whole idea. The columns are not stored anywhere — they are worked out by counting commas. Line one has two commas, so it has three values, so the table has three columns.

Key Takeaway
CSV stands for Comma Separated Values. One line per row, and the values in that line separated by commas. Nothing else.

Look at the first line again: Roll,Name,Marks. Those are not a student — they are the column names. A line like that at the top of the file is called the header, and it is what lets a person open the file and know what the numbers mean.

marks.csv
Roll,Name,Marks
1,Ravi,78
2,Meera,91
3,Amit,65
Nothing marks the header as special
It is an ordinary line of text like any other. Python will hand it to you along with the records unless you tell it not to, and a program that forgets counts four students where there are three. Every lesson in this chapter comes back to this.

4The file must be named .csv

A CSV file is saved with the extension .csv marks.csv, students.csv, sales_2025.csv. That extension is how the rest of your computer knows what the file is: double-click a .csv and Excel opens it as a grid, while the same file named marks.txt opens in Notepad as four lines of text.

marks.csv

Excel, Google Sheets and Python all recognise it as a table.

marks.txt

Exactly the same characters inside — but nothing treats it as a table.

Note
The contents of the two files above are identical. The extension changes nothing inside the file; it changes what other programs assume about it. So name it .csv, and it will behave like a CSV everywhere.

5It is still a text file

Open marks.csv in Notepad and you can read every character of it. There is nothing hidden, nothing encoded, no special format — which is exactly why so many different programs can read one. A CSV is an ordinary text file that follows one extra rule about where the commas go.

Careful: .csv and .xlsx are not the same
Both open in Excel and both look like a grid. A .csv is a text file — just values and commas. An .xlsx is a binary file, and it has to be, because it also stores formulas, colours, fonts, charts and several sheets. A CSV can hold none of those. It holds values, and that is the trade: it loses the formatting and gains the ability to be opened by anything.

6The three, side by side

You now know three ways of putting data in a file. Each is good at something different, and the exam likes asking which you would choose:

Let's Recap!
Text fileBinary fileCSV file
What it holdsany text at allraw bytesa table
Readable in Notepadyesnoyes
Opens in Excelnonoyes
Readable by other programsyesonly the one that wrote ityes
Numbers come back astextnumberstext
Best fornotes, logs, paragraphsone program's own private datatables shared with anything
CSV is not the best at everything
Look at the row about numbers. A CSV loses the difference between 78 and "78", exactly as a text file does — because it is a text file. A binary file is still the only one of the three that gives a number back as a number. What CSV buys with that loss is that everybody else can open the file.

7Where you actually meet CSV files

Marks and attendance

Every school management system exports a class list as CSV, because every other program can read one.

Bank statements

Download a statement and you are usually offered PDF or CSV. The CSV is the one you can total up yourself.

Moving data between programs

Excel writes it, Python reads it, a website loads it. CSV is the common language none of them owns.

Datasets for Pandas

pd.read_csv() is how almost every data project starts — including the ones in the IP course.

8Recap

CSV = Comma Separated Values

One of the most popular ways of keeping tabular data in a text file.

One line per row

And inside the line, a comma between each pair of values.

The columns are counted, not stored

Two commas mean three values. Nothing in the file names the columns except the header line.

The first line is usually the header

The column names. It is an ordinary line, so your program has to remember it is there.

Save it as .csv

The extension is how Excel, Python and everything else recognise the file as a table.

It is a text file

Readable in Notepad, character for character. Not a third kind of file — a text file with one extra rule.

Quick Check

What does CSV stand for?

Quick Check

A CSV file is which kind of file?

Quick Check

A line in a CSV file reads 4,Sneha,88. How many columns does the table have?

Quick Check

What extension should a CSV file be saved with?