Course topics

By WebNest Studio Engineering Team

Python Tutorial

Reading and Writing CSV Files in Python

CSV (comma-separated values) is the universal exchange format for tabular data: exports from Excel and Google Sheets, bank statements, database dumps, reports. Python's built-in csv module reads and writes CSV correctly — handling quoted fields, commas inside values, and different delimiters — which naive split(",") code gets wrong.

This lesson covers csv.reader and csv.writer, the dictionary-based DictReader and DictWriter, delimiters and quoting, encodings for Excel, processing large files, and when to use pandas instead.

Why it matters

CSV is the common format for moving table data between spreadsheets, databases and programs: bank statements, exported reports, product lists. It looks simple enough to parse by splitting on commas, until a value contains a comma or a quotation mark. The csv module handles those cases correctly.

reader and writer

Open the file with newline="" (required by the csv module to handle line endings correctly) and an explicit encoding. csv.reader(f) yields each row as a list of strings; csv.writer(f) writes rows with writerow() and writerows(), quoting values that contain commas or quotes automatically. Remember that all values come back as strings — convert numbers yourself.

DictReader and DictWriter

DictReader uses the header row as keys, giving you row["email"] instead of row[2] — robust against column reordering. DictWriter(f, fieldnames=[...]) writes dictionaries; call writeheader() first. extrasaction="ignore" skips dict keys that are not columns.

Dialects, Delimiters and Encodings

Use delimiter=";" or "\t" for semicolon- or tab-separated files, and quoting=csv.QUOTE_ALL or QUOTE_NONNUMERIC to control quoting. Excel on Windows opens UTF-8 CSV files correctly when written with encoding="utf-8-sig" (UTF-8 with a BOM) — important for names and the ₹ symbol.

Large Files and pandas

The csv module streams row by row, so it handles files larger than memory. For analysis — grouping, filtering, joining — pandas.read_csv() is more convenient (see the pandas lesson).

Examples

Writing and reading with csv.writer and csv.reader

Python
import csv

rows = [
    ["name", "city", "marks"],
    ["Asha", "Pune", 91],
    ["Ravi Kumar", "New Delhi, India", 72],
    ['Meera "M"', "Mumbai", 88],
]
with open("students.csv", "w", newline="", encoding="utf-8") as f:
    csv.writer(f).writerows(rows)

print(open("students.csv", encoding="utf-8").read())

with open("students.csv", newline="", encoding="utf-8") as f:
    reader = csv.reader(f)
    header = next(reader)
    for name, city, marks in reader:
        print(f"{name:<12} {city:<18} {int(marks) + 5}")
Output
name,city,marks
Asha,Pune,91
Ravi Kumar,"New Delhi, India",72
"Meera ""M""",Mumbai,88

Asha         Pune               96
Ravi Kumar   New Delhi, India   77
Meera "M"    Mumbai             93

DictReader and DictWriter

Python
import csv

orders = [
    {"order_id": "WN-1", "customer": "Asha", "amount": 2999, "internal_note": "vip"},
    {"order_id": "WN-2", "customer": "Ravi", "amount": 499, "internal_note": ""},
]
with open("orders.csv", "w", newline="", encoding="utf-8") as f:
    writer = csv.DictWriter(f, fieldnames=["order_id", "customer", "amount"], extrasaction="ignore")
    writer.writeheader()
    writer.writerows(orders)

with open("orders.csv", newline="", encoding="utf-8") as f:
    reader = csv.DictReader(f)
    print(reader.fieldnames)
    total = 0
    for row in reader:
        total += int(row["amount"])
        print(row["order_id"], row["customer"])
print("total:", total)
Output
['order_id', 'customer', 'amount']
WN-1 Asha
WN-2 Ravi
total: 3498

Semicolons, tabs, quoting and Excel-friendly UTF-8

Python
import csv

with open("prices_eu.csv", "w", newline="", encoding="utf-8") as f:
    w = csv.writer(f, delimiter=";", quoting=csv.QUOTE_NONNUMERIC)
    w.writerow(["item", "price"])
    w.writerow(["Kaffee", 3.5])
print(open("prices_eu.csv", encoding="utf-8").read().strip())

with open("report_excel.csv", "w", newline="", encoding="utf-8-sig") as f:
    csv.writer(f).writerow(["Course", "Price"])
with open("report_excel.csv", "rb") as f:
    print(f.read()[:3])

tsv = "name\tmarks\nAsha\t91\n"
print(list(csv.reader(tsv.splitlines(), delimiter="\t")))
Output
"item";"price"
"Kaffee";3.5
b'\xef\xbb\xbf'
[['name', 'marks'], ['Asha', '91']]

Common Mistakes

  • Splitting lines on "," manually, which breaks on quoted fields containing commas.
  • Opening files without newline="", producing blank lines between rows on Windows.
  • Forgetting that every value read from CSV is a string.
  • Writing UTF-8 without a BOM and seeing garbled characters when opening in Excel.

Exercise

The CSV text is read from memory here in place of a file. Use csv.DictReader to print the name of each student who scored more than 85, and then the total of all scores.

Expected output: Ravi then 173

Python · starter code
import csv
import io

text = "name,score\nAsha,82\nRavi,91\n"
file = io.StringIO(text)  # behaves like an open file

total = 0

# TODO: read the rows with csv.DictReader
# TODO: print the name when the score is above 85, and add every score to total

print(total)
Show hints
  • Each row from DictReader is a dictionary keyed by the header row.
  • Values read from CSV are strings, so convert the score with int().
Show solution
Python · solution
import csv
import io

text = "name,score\nAsha,82\nRavi,91\n"
file = io.StringIO(text)  # behaves like an open file

total = 0

for row in csv.DictReader(file):
    score = int(row["score"])
    if score > 85:
        print(row["name"])
    total += score

print(total)

Quick quiz

1. What type are the values read by csv.reader?

  • A. Numbers when the text looks like a number
  • B. Always floats
  • C. Always strings
  • D. Bytes
Show answer

C. The csv module does no conversion. Numbers must be converted explicitly.

2. Where does csv.DictReader get its keys from by default?

  • A. They must be supplied
  • B. The column numbers
  • C. The file name
  • D. The first row of the file
Show answer

D. The header row provides the field names.

3. Why is a file opened with newline="" when it is passed to the csv module?

  • A. So that the csv module controls line endings and no blank rows appear on Windows
  • B. To make it faster
  • C. To skip the header
  • D. To allow commas in values
Show answer

A. This is the form recommended in the documentation for both reading and writing.

Interview questions

Why use the csv module instead of line.split(",")?

Show answer

A value may itself contain a comma, a quotation mark or a line break, in which case it is enclosed in quotes: "Sharma, Asha",82. Splitting on commas breaks such a value into two. The csv module follows the quoting rules, and handles other delimiters and dialects, when both reading and writing.

How would you process a CSV file that is too large to fit in memory?

Show answer

Iterate over the reader, which yields one row at a time, and keep only running results such as totals or counts, never a list of all rows. With pandas, read_csv(..., chunksize=n) returns the file in pieces of n rows that are processed one after another.

Key Points to Remember

  • Open CSV files with newline="" and an explicit encoding.
  • csv.reader/writer handle quoting and embedded commas correctly.
  • DictReader/DictWriter work with column names instead of positions.
  • Set delimiter and quoting for other formats; utf-8-sig for Excel.
  • Use pandas.read_csv for analysis-heavy work.

Practice in the playground

Write and run your own code for this topic, or start from the exercise or an example. Runs Python in your browser. Your code for each topic is kept in this browser.

Run the examples

Change an input, predict the result, then compare it with the output. Explain why the result changes.

WebNest Studio

Written by

WebNest Studio Engineering Team

Practical lessons from developers working with Java, Python, React, databases and production systems.

Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering