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
Run in browser
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.