Read CSV File in Python

Read a CSV file and print each row using the csv module.

BeginnerFile Handling ProgramsExample 4 of 20
read-csv-file.py
1# Program to read a CSV file
2
3import csv
4
5filename = input("Enter CSV filename: ")
6
7try:
8 with open(filename, newline="", encoding="utf-8") as f:
9 reader = csv.reader(f)
10 for row in reader:
11 print(row)
12except FileNotFoundError:
13 print("CSV file not found.")

Output

Enter CSV filename: data.csv
['name', 'age']
['Alice', '25']
['Bob', '30']

What's going on

Uses csv.reader to parse comma-separated values into Python lists per row.

After this program

If the idea is still fuzzy, read Day 14: File Handling in the Python course. For a short check, try the Python quiz (300 questions). Stay with all 20 file handling programs, or see every topic on Python exercises.