How to Read a CSV File in Python

If you want to read a CSV file in Python, the simplest and most reliable way is to use the built-in csv module.

This page shows you how to:

  • Read rows from a CSV file in Python
  • Understand why the csv module is better than split(',') for real CSV data
  • Loop through rows safely
  • Read CSV files with or without headers

Quick answer

import csv

with open('data.csv', newline='', encoding='utf-8') as file:
    reader = csv.reader(file)
    for row in reader:
        print(row)

Use csv.reader() for basic CSV files. newline='' helps the csv module read rows correctly.

When to use the csv module

Use the csv module when:

  • Your file contains comma-separated values
  • You want Python to handle quoted text correctly
  • You do not want to install any extra package

The csv module is part of Python's standard library, so it works without installation.

It is better than manually splitting lines with split(',') because real CSV files can contain commas inside quoted values.

For example, this row:

Alice,"New York, USA",30

should be read as 3 values, not 4. The csv module handles that correctly.

Basic way to read a CSV file

The basic process is:

  • Open the file with open()
  • Pass the file object to csv.reader()
  • Loop through the reader one row at a time
  • Use each row as a list of strings
import csv

with open('data.csv', newline='', encoding='utf-8') as file:
    reader = csv.reader(file)

    for row in reader:
        print(row)

What this code does

  • open('data.csv', ...) opens the file
  • csv.reader(file) creates a CSV reader
  • Each row is a list
  • Every value in the row is read as a string

If data.csv contains:

name,age,city
Alice,30,London
Bob,25,Paris

the output will be:

['name', 'age', 'city']
['Alice', '30', 'London']
['Bob', '25', 'Paris']

If you are not familiar with opening files yet, see how to read a file in Python and the Python open() function explained.

Read a CSV file with headers

If the first row contains column names, csv.DictReader() is often easier to use.

It returns each row as a dictionary, so you can access values by column name.

import csv

with open('data.csv', newline='', encoding='utf-8') as file:
    reader = csv.DictReader(file)

    for row in reader:
        print(row)
        print(row['name'], row['age'])

If the file contains:

name,age,city
Alice,30,London
Bob,25,Paris

the output will be:

{'name': 'Alice', 'age': '30', 'city': 'London'}
Alice 30
{'name': 'Bob', 'age': '25', 'city': 'Paris'}
Bob 25

This is useful when you want to work with named columns instead of remembering index positions like row[0] or row[1].

If you want a safer way to read dictionary values, see Python dict get() method.

Important file options

When reading CSV files, these options matter:

  • Use newline='' when opening the file
  • Use encoding='utf-8' for common text files
  • Use the correct file path if the file is in another folder

Example:

import csv

with open('files/data.csv', newline='', encoding='utf-8') as file:
    reader = csv.reader(file)
    for row in reader:
        print(row)

Why newline='' matters

The csv module expects the file to be opened with newline=''. This helps it handle line endings correctly across different systems.

Why encoding='utf-8' matters

Many CSV files use UTF-8 text encoding. If your file contains special characters, this setting often prevents decoding problems.

If you get a text decoding error, see how to fix UnicodeDecodeError: utf-8 codec can't decode byte.

Using the right path

If Python cannot find your file, the problem is often the path, not the CSV code.

For example:

import csv

with open('data/report.csv', newline='', encoding='utf-8') as file:
    reader = csv.reader(file)
    for row in reader:
        print(row)

If the file does not exist in that location, Python will raise an error. For help with that, see how to fix FileNotFoundError: No such file or directory.

Common beginner mistakes

Here are some common problems when reading CSV files:

  • Using split(',') instead of the csv module
  • Forgetting that values are read as strings
  • Using the wrong file path
  • Not handling files with headers correctly

Mistake: using split(',')

This may seem simpler, but it breaks on valid CSV data with quoted commas.

line = 'Alice,"New York, USA",30'
print(line.split(','))

Output:

['Alice', '"New York', ' USA"', '30']

That is wrong for CSV data.

Mistake: expecting numbers instead of strings

CSV data is text. Even numbers are read as strings.

import csv

with open('data.csv', newline='', encoding='utf-8') as file:
    reader = csv.DictReader(file)

    for row in reader:
        age = int(row['age'])
        print(age + 1)

Use int() or float() when you need numeric values.

Mistake: not handling the header row

With csv.reader(), the first row is just another row. If your file has headers, you may want to skip that first row.

import csv

with open('data.csv', newline='', encoding='utf-8') as file:
    reader = csv.reader(file)

    next(reader)  # skip header row

    for row in reader:
        print(row)

If your file has headers, csv.DictReader() is usually the easier choice.

Simple debugging steps

If your CSV code is not working, try these quick checks.

print(row)
print(type(row))

This helps you confirm what Python is reading.

Check the current working folder

import os
print(os.getcwd())

This shows the folder Python is using for relative paths like 'data.csv'.

Check whether the file exists

import os
print(os.path.exists('data.csv'))

If this prints False, your path is wrong or the file is not where you think it is.

Common causes of CSV reading problems

  • Trying to read a CSV file with plain string splitting
  • Using a relative path that points to the wrong folder
  • Forgetting to skip or handle the header row
  • Expecting numbers but getting strings
  • Opening a file with the wrong encoding

FAQ

Do I need to install anything to read CSV files in Python?

No. You can use the built-in csv module.

Why not use split(',') to read a CSV file?

It breaks on quoted commas and other valid CSV formatting. csv.reader() handles this correctly.

Why are all CSV values strings?

csv.reader() reads text from the file. Convert values with int() or float() when needed.

How do I read columns by name?

Use csv.DictReader() so each row becomes a dictionary.

See also