Read CSV File

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

PythonBeginner
Python
# Program to read a CSV file

import csv

filename = input("Enter CSV filename: ")

try:
    with open(filename, newline="", encoding="utf-8") as f:
        reader = csv.reader(f)
        for row in reader:
            print(row)
except FileNotFoundError:
    print("CSV file not found.")

Output

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

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