7.1 What is a CSV file?
Each line is one record (one row). Inside a line, fields (columns) are separated by a comma. Here is marks.csv as it sits on disk — open it in Notepad and you’ll see exactly this:
The first line is an optional header — it names the columns. The rest are data rows. Open the same file in Excel / LibreOffice and it appears as a proper table.
7.2 Why do we use CSV?
- Readable — any editor can show it; no special software needed.
- Portable — works on every operating system and every programming language.
- Compact — no XML tags, no extra metadata.
- Directly understood by Excel / Google Sheets — double-click and it opens as a table.
- Easy to back up, diff, email — it is just text.
7.2.1 CSV vs Text vs Binary — at a glance
| Text (.txt) | CSV (.csv) | Binary (.dat) | |
|---|---|---|---|
| Structure | Free-form lines | Rows & columns | Raw bytes |
| Opens in Excel? | No (plain view) | Yes (as a table) | No |
| Keeps Python types? | No — always str | No — always str | Yes (with pickle) |
| Ideal for | Logs, notes | Spreadsheet exchange | Images, Python objects |
7.3 The csv Module
Python ships with a built-in module called csv that reads and writes CSV correctly — handling edge cases like commas inside a quoted field, newlines inside a field, and different separators.
7.3.1 Why not just use split(",")?
For the simple files in your CBSE practicals you technically could. But consider a single row like:
"Ankit Kumar, Jr." has a comma inside quotes — it is still one field. Calling .split(",") would wrongly break it into four pieces. The csv module understands quoting, so always prefer it.
7.4 Writing a CSV File
7.4.1 Step 1 — open the file and create a writer
newline="" when opening a CSV file (both for reading and writing). On Windows, without it, Python writes extra \r characters — producing blank lines between every row when Excel opens the file.
7.4.2 writer.writerow(row_list) — write one row
Takes a list (or tuple) of values and writes them as one comma-separated line, adding the newline for you.
7.4.3 writer.writerows(list_of_rows) — write many rows
writerow = one row at a time. writerows = everything in one shot (takes a list of rows). Do not mix them up — writerows with a single row writes each character as a separate column.
7.4.4 Appending rows — mode "a"
7.5 Reading a CSV File
7.5.1 csv.reader — iterate over rows
int() / float() before doing math.
7.5.2 Skipping the header row
7.5.3 Converting columns as you read
7.6 Custom Delimiters — TSV, semicolons, pipes
CSV’s “C” stands for comma, but the separator is configurable. European spreadsheets often use ; because they write decimals with a comma; log files sometimes use TAB (\t) or |.
7.6.1 Other useful csv options
| Option | What it does |
|---|---|
delimiter | Column separator (default ",") |
quotechar | Character that wraps text fields (default '"') |
quoting | When to quote — csv.QUOTE_ALL, QUOTE_MINIMAL, … |
lineterminator | String written at the end of each row |
7.7 DictReader & DictWriter — readable, self-documenting code
Instead of accessing columns by index (row[0], row[1] …) — which is easy to confuse — you can access them by column name.
7.7.1 csv.DictReader
7.7.2 csv.DictWriter
DictReader / DictWriter whenever the file has a header row — your code becomes self-documenting, and adding / reordering columns later does not break it.
7.8 CBSE-style Worked Programs
7.8.1 Create a CSV of student records
7.8.2 Display every record in the CSV
7.8.3 Count records where marks > 75
7.8.4 Search a student by roll number
7.8.5 Append a new student interactively
7.8.6 Update marks for a given roll number
Like binary files, CSV updates follow the read-all → modify → write-all pattern.
7.8.7 Copy passing students (marks ≥ 33) to another CSV
7.8.8 Class topper from the CSV
7.8.9 Merge two CSVs into one
7.8.10 CSV → text summary report
7.9 Common Mistakes to Avoid
| # | Mistake | Fix |
|---|---|---|
| 1 | Missing newline="" while opening | Always pass it — blank lines vanish |
| 2 | Forgetting that every value read is a string | Convert with int() / float() before math |
| 3 | writerows(single_row) — each character becomes a column | Use writerow for one row |
| 4 | Forgetting next(r) — header is treated as data | Call next(r) once to skip it |
| 5 | Using split(",") on rows with quoted commas | Use the csv module |
| 6 | Writing in mode "w" when you meant "a" | "w" wipes; "a" appends |
| 7 | Wrong delimiter (file uses ;) | Pass delimiter=";" to reader / writer |
| 8 | Trying to update a row in the middle of the file | Use the read-all / modify / write-all pattern |
Quick-revision summary
- A CSV is a plain-text, comma-separated table — the universal format between Python, Excel and databases.
import csv; open the file withnewline=""— always.csv.writer(f).writerow(row)writes one row;writerows(list_of_rows)writes many at once.csv.reader(f)yields each row as a list of strings; convert numeric columns before math.- Skip the header with
next(reader). DictReader/DictWriterlet you access columns by name — recommended when the file has a header.- Custom separators: pass
delimiter=";"(or any other character). - Updating rows follows the read-all → modify → write-all pattern, just like binary files.