Python CSV Reader Example
This example shows how to read a CSV file in Python with the built-in csv module.
It focuses on one simple working script:
- open a CSV file
- read each row
- print the row
- understand what the output means
If you want the fastest working example, start here:
import csv
with open('data.csv', newline='', encoding='utf-8') as file:
reader = csv.reader(file)
for row in reader:
print(row)
Use this when you want the quickest way to read rows from a CSV file.
What this example shows
In this page, you will learn:
- how to open a CSV file safely with
with open(...) - how to use
csv.reader()to read rows - how each row becomes a list of values
- when this simple approach is enough for beginners
This is a good starting example if you want to see CSV reading work before moving to a more detailed guide like how to read a CSV file in Python.
Example CSV file to use
Create a file named data.csv in the same folder as your Python script.
Put this content inside it:
name,age,city
Alice,30,London
Bob,25,Paris
Charlie,35,Berlin
This small file makes it easy to test the code right away.
Basic CSV reader script
Here is the full script again:
import csv
with open('data.csv', newline='', encoding='utf-8') as file:
reader = csv.reader(file)
for row in reader:
print(row)
What each part does
import csv
- loads Python's built-in CSV tools
- without this line,
csv.reader()will not work
open('data.csv', newline='', encoding='utf-8')
- opens the file for reading
encoding='utf-8'helps Python read text correctlynewline=''is the recommended way to open CSV files with thecsvmodule
with open(...) as file
- opens the file safely
- closes it automatically when finished
reader = csv.reader(file)
- creates a CSV reader object
- this lets you loop through the file row by row
for row in reader:
- reads one row at a time
print(row)
- prints each row so you can see what Python read
If you want to understand open() better, see Python open() function explained and Python file handling basics.
How to understand the output
For the sample CSV file above, the script prints:
['name', 'age', 'city']
['Alice', '30', 'London']
['Bob', '25', 'Paris']
['Charlie', '35', 'Berlin']
A few important things to notice:
- each row is a Python list
- the first row is the header row
- all values are read as strings by default
That means:
'30'is a string, not a number'25'is also a string- if you need numbers, you must convert them yourself
For example, if you later want to turn '30' into 30, see how to convert string to int in Python.
You can also test what type each row is:
import csv
with open('data.csv', newline='', encoding='utf-8') as file:
reader = csv.reader(file)
for row in reader:
print(row)
print(type(row))
Expected type:
<class 'list'>
Skipping the header row
Often, the first row contains column names like name, age, and city.
If you want to skip that row, call next(reader) once before the loop:
import csv
with open('data.csv', newline='', encoding='utf-8') as file:
reader = csv.reader(file)
next(reader) # Skip the header row
for row in reader:
print(row)
Output:
['Alice', '30', 'London']
['Bob', '25', 'Paris']
['Charlie', '35', 'Berlin']
This is useful when you only want the real data rows.
Reading CSV rows as dictionaries
csv.reader() gives you lists.
If you want to access values by column name instead of position, use csv.DictReader():
import csv
with open('data.csv', newline='', encoding='utf-8') as file:
reader = csv.DictReader(file)
for row in reader:
print(row['name'], row['age'], row['city'])
Output:
Alice 30 London
Bob 25 Paris
Charlie 35 Berlin
This is often easier for beginners because:
row['name']is clearer thanrow[0]- column names become dictionary keys
- you do not need to manually skip the header row
If you want a broader overview, see the Python csv module overview.
Common beginner mistakes
Here are some common problems when reading CSV files.
Wrong file path
If Python cannot find data.csv, you may get a FileNotFoundError.
Example:
with open('data.csv', newline='', encoding='utf-8') as file:
...
This only works if data.csv is in the folder Python expects.
To check your current working folder:
import os
print(os.getcwd())
To check whether the file exists:
import os
print(os.path.exists('data.csv'))
If you need help with this error, see FileNotFoundError: No such file or directory fix or how to check if a file exists in Python.
Forgetting to import csv
This will not work:
with open('data.csv', newline='', encoding='utf-8') as file:
reader = csv.reader(file)
You must include:
import csv
Expecting numbers instead of strings
CSV files store text.
So this row:
['Alice', '30', 'London']
contains '30' as a string.
You can confirm that with debugging prints:
import csv
with open('data.csv', newline='', encoding='utf-8') as file:
reader = csv.reader(file)
for row in reader:
print(row)
print(len(row))
Not handling the header row
If your first printed row is:
['name', 'age', 'city']
that is the header, not actual data.
Use next(reader) if you want to skip it.
Wrong file or wrong folder
Sometimes the script runs, but you are reading the wrong CSV file.
This can happen if:
- the file is in another folder
- you have multiple files with similar names
- your editor runs the script from a different location
Simple checks like these can help:
import os
print(os.getcwd())
print(os.path.exists('data.csv'))
Wrong delimiter
Some CSV-like files use semicolons instead of commas.
For example:
name;age;city
Alice;30;London
If that happens, csv.reader() may not split the row the way you expect.
You would need to pass a delimiter:
import csv
with open('data.csv', newline='', encoding='utf-8') as file:
reader = csv.reader(file, delimiter=';')
for row in reader:
print(row)
When to use this example vs other pages
Use this page when you want:
- one full working CSV reader example
- a quick way to test reading rows
- a simple starting point before learning more
Use other pages when you need something more specific:
- how to read a CSV file in Python for step-by-step task guidance
- Python csv module overview for a broader look at CSV tools
- Python open() function explained for deeper understanding of file opening
- Python file handling basics for the bigger picture of working with files
FAQ
Why does each row look like a list?
csv.reader() returns each row as a list of strings.
Why are numbers read as strings?
CSV files store plain text, so Python reads values as strings unless you convert them.
How do I skip the first row?
Call next(reader) once before looping through the remaining rows.
When should I use DictReader instead?
Use csv.DictReader() when you want to access values by column name instead of index position.
See also
- How to read a CSV file in Python
- Python open() function explained
- Python file handling basics: read and write
- FileNotFoundError: No such file or directory fix
- How to check if a file exists in Python
- How to convert string to int in Python
- Python CSV writer example
Try the script with the sample data.csv file first.
Once that works, move on to how to read a CSV file in Python if you want to read specific columns or handle more realistic CSV data.