Editor

Python Program to Read a CSV File

Beginner · File handling

A CSV (comma separated values) file stores a table as plain text, one row per line with commas between the values, and spreadsheet programs like Excel can open it. This program creates a small CSV file of student marks, reads it with Python's csv module, and prints each row along with the class average.

Program

read_csv_file.py
import csv
# Make a sample file to work on
with open("marks.csv", "w", newline="") as f:
f.write("name,maths,science\n")
f.write("Aarav,88,92\n")
f.write("Diya,95,89\n")
f.write("Kabir,76,81\n")
with open("marks.csv", "r", newline="") as f:
reader = csv.reader(f)
header = next(reader) # the first row holds the column names
print("Columns:", header)
total = 0
count = 0
for row in reader: # each row is a list of strings
name = row[0]
maths = int(row[1]) # turn the text into a number
science = int(row[2])
print(f"{name}: maths {maths}, science {science}")
total += maths
count += 1
print("Class average in maths:", round(total / count, 1))
Output
Columns: ['name', 'maths', 'science']
Aarav: maths 88, science 92
Diya: maths 95, science 89
Kabir: maths 76, science 81
Class average in maths: 86.3

How it works

  1. The sample file is written as plain text, so you can see exactly what a CSV file looks like: a header line, then one line per student.
  2. csv.reader(f) turns each line into a list of values, split at the commas.
  3. next(reader) takes the first row, the column names, so the loop only sees the student rows.
  4. Everything read from a CSV file is a string, so int(row[1]) turns the marks into numbers before they are added.
  5. The csv module also copes with values that contain commas themselves, wrapped in quotes, which a plain line.split(",") gets wrong.

Other ways to write it

Using csv.DictReader

Using csv.DictReader
import csv
# Make a sample file to work on
with open("marks.csv", "w", newline="") as f:
f.write("name,maths,science\nAarav,88,92\nDiya,95,89\nKabir,76,81\n")
with open("marks.csv", "r", newline="") as f:
for row in csv.DictReader(f): # each row is a dictionary
total = int(row["maths"]) + int(row["science"])
print(row["name"], "scored", total, "out of 200")
Output
Aarav scored 180 out of 200
Diya scored 184 out of 200
Kabir scored 157 out of 200

DictReader uses the header row as the keys, so row["maths"] names the column instead of relying on its position in the row.

Without the csv module

Without the csv module
# Make a sample file to work on
with open("marks.csv", "w") as f:
f.write("name,maths,science\nAarav,88,92\nDiya,95,89\n")
with open("marks.csv", "r") as f:
lines = f.read().splitlines()
for line in lines[1:]: # skip the header line
name, maths, science = line.split(",")
print(name, "- maths:", maths, "science:", science)
Output
Aarav - maths: 88 science: 92
Diya - maths: 95 science: 89

Splitting each line at the commas works for simple files like this one, but breaks as soon as a value contains a comma, such as "Sharma, Riya", so the csv module is the safer choice.

Try it yourself

Add a fourth student to the sample file and also print the class average in science.

Edit the program above and press Run, or open it in the full editor to save it and share it. To watch it run line by line, open it in the visualizer.

All Python programs · CBSE class 12 practical file · Python exercises