Ravindra BagaleCourses & study guides मराठी Track your progress

How to Read and Write CSV, JSON and Text Files in Python (with open and with)

P9.3 How to Read a CSV File in Python with the csv Module

Why. CSV is the most common data export from banks, shops and apps. pandas reads it in one line, but knowing the basics helps you debug strange files.

What. csv.DictReader reads each row as a dictionary using the header line as keys. All values arrive as text.

How.

Read a CSV

  1. Open the file with newline="" and utf-8.
  2. Create csv.DictReader(f).
  3. Loop over rows.
  4. Convert numbers with int()/float().

Worked example A — Read rows as dictionaries.

Step 1 — What we do. We read a small sales file from a grocery chain in Maharashtra.

Step 2 — Make the input file sales.csv in the same folder as your code:

date,store,item,qty,price
2026-03-01,Pune,Milk,3,56
2026-03-01,Mumbai,Bread,2,40
2026-03-02,Pune,Eggs,1,84
2026-03-02,Nagpur,Milk,5,56

About this data: each line after the first is one sale. The first line is the header: date of sale, store city, item name, qty (units sold) and price (₹ per unit).

Step 3 — Type and run this code.

import csv
with open("sales.csv", newline="", encoding="utf-8") as f:
    for row in csv.DictReader(f):
        print(row["store"], row["item"], row["qty"])

Step 4 — Output you should see.

Pune Milk 3
Mumbai Bread 2
Pune Eggs 1
Nagpur Milk 5

Step 5 — What it means. Each row is a dict. Keys come from the header.

Worked example B — Calculate revenue per row and total.

Step 1 — What we do. qty and price are text, so we convert before multiplying.

Step 2 — Make the input file sales.csv in the same folder as your code:

date,store,item,qty,price
2026-03-01,Pune,Milk,3,56
2026-03-01,Mumbai,Bread,2,40
2026-03-02,Pune,Eggs,1,84
2026-03-02,Nagpur,Milk,5,56

About this data: each line after the first is one sale. The first line is the header: date of sale, store city, item name, qty (units sold) and price (₹ per unit).

Step 3 — Type and run this code.

import csv
total = 0
with open("sales.csv", newline="", encoding="utf-8") as f:
    for row in csv.DictReader(f):
        revenue = int(row["qty"]) * float(row["price"])
        total += revenue
print(f"Total revenue: ₹{total:,.2f}")

Step 4 — Output you should see.

Total revenue: ₹612.00

Step 5 — What it means. Without int() and float(), Python would try to multiply text. pandas converts types for you; here we see it by hand.

Worked example C — Revenue by store.

Step 1 — What we do. We group by store with a dictionary.

Step 2 — Make the input file sales.csv in the same folder as your code:

date,store,item,qty,price
2026-03-01,Pune,Milk,3,56
2026-03-01,Mumbai,Bread,2,40
2026-03-02,Pune,Eggs,1,84
2026-03-02,Nagpur,Milk,5,56

About this data: each line after the first is one sale. The first line is the header: date of sale, store city, item name, qty (units sold) and price (₹ per unit).

Step 3 — Type and run this code.

import csv
by_store = {}
with open("sales.csv", newline="", encoding="utf-8") as f:
    for row in csv.DictReader(f):
        rev = int(row["qty"]) * float(row["price"])
        by_store[row["store"]] = by_store.get(row["store"], 0) + rev
for store, rev in by_store.items():
    print(store, rev)

Step 4 — Output you should see.

Pune 252.0
Mumbai 80.0
Nagpur 280.0

Step 5 — What it means. This is a groupby by hand. pandas: df.groupby("store")["revenue"].sum().

Ravindra Bagale's Tip

When pandas reads a file strangely, open it with plain Python and print the first 3 lines. You will see the real separator and header.