What a CSV file is and why Python reads it
A CSV file is a plain text document where data sits in rows and columns, separated by commas. CSV stands for "comma-separated values." When you open one in a text editor, you see something like this:
name,age,city Sarah,28,Portland James,34,Denver Maria,31,Austin
Python reads CSV files because they are one of the most common ways data gets stored and shared. A spreadsheet program like Excel or Google Sheets can save its data as a CSV file, and Python can then process that data — sort it, filter it, do math with it, or combine it with other information. Python does not need special software to read a CSV; it just needs the right instructions.
The simplest way to read a CSV file in Python is to use the built-in csv module, which is a set of tools that comes with Python automatically. You do not have to read anything extra.
Key Takeaways
- Python's csv module reads CSV files by opening the file, then looping through each row one at a time.
- The csv.reader() function turns each row into a list of values separated by commas, which you can then work with.
- The csv.DictReader() function is easier if your CSV has a header row — it turns each row into a dictionary where you can call values by their column name instead of their position.
- You must open the file with open() before you can read it, and close it when you are done to avoid leaving it locked.
- The pandas library is an alternative that works faster with large files or complex data, but csv is the right starting point for most tasks.
Opening a CSV file and reading it row by row
To read a CSV file, you first tell Python where the file is, then you tell it to read the contents. Here is the most basic version:
import csv with open('data.csv', 'r') as file: reader = csv.reader(file) for row in reader: print(row)
The first line tells Python to load the csv module. The second line opens the file called 'data.csv' in read mode (the 'r' means read). The with statement is important — it automatically closes the file when you are done, so you do not accidentally leave it open.
Inside the with block, csv.reader() takes the file and prepares it for reading. Then the for loop goes through each row one at a time. Each row becomes a list — so if your CSV has three columns, each row is a list with three items. When you print a row, you see something like ['Sarah', '28', 'Portland'].
If you want to access a specific column from a row, you use its position number in square brackets. The first column is position 0, the second is position 1, and so on. So row[0] gives you the first column's value, row[1] gives you the second, and so on.
Using DictReader when your CSV has column headers
Most CSV files have a header row at the top that names each column. If your file looks like this:
name,age,city Sarah,28,Portland James,34,Denver
Then csv.DictReader() is easier to work with than csv.reader(). DictReader treats each row as a dictionary — a structure where you can look up values by their column name instead of their position:
import csv with open('data.csv', 'r') as file: reader = csv.DictReader(file) for row in reader: print(row['name'], row['age'])
Now each row is a dictionary where row['name'] gives you the value from the name column, row['age'] gives you the age column, and so on. This is less error-prone than remembering that name is column 0 and age is column 1. DictReader automatically reads the first row as the header and uses those names as the keys.
Storing the data in a list so you can use it later
If you just print each row as you read it, the data disappears once the loop ends. To keep the data around, store it in a list:
import csv data = [] with open('data.csv', 'r') as file: reader = csv.DictReader(file) for row in reader: data.append(row)
Now data is a list of dictionaries. Each dictionary is one row from your CSV. You can loop through it again later, filter it, sort it, or pass it to another part of your program. The data stays in memory until your program ends.
If your CSV is very large — thousands or millions of rows — storing everything in a list can use a lot of memory. In that case, it is better to process each row as you read it and only keep the results you need.
Handling common problems: missing values and special characters
CSV files sometimes have empty cells, or they contain commas inside the data itself. For example, if a city name is "San Francisco, CA", that comma inside the value can confuse the reader.
Most CSV files handle this by putting quotes around values that contain commas. So the row looks like: Sarah,28,"San Francisco, CA". Python's csv module handles this automatically — you do not have to do anything special.
For empty cells, csv.reader() and csv.DictReader() will give you an empty string (nothing). If you want to treat empty cells as a specific value — like 0 or "unknown" — you can check for it after reading:
if row['age'] == '': row['age'] = 'unknown'
Some CSV files use different separators instead of commas — semicolons or tabs, for example. If your file uses semicolons, tell the reader:
reader = csv.reader(file, delimiter=';')
When to use pandas instead of the csv module
The csv module works well for most tasks, but if you are working with a very large file or you need to do complex operations — like sorting by multiple columns, combining data from two files, or doing calculations across rows — the pandas library is faster and easier.
Pandas is not built into Python, so you have to read it first. Once you do, reading a CSV takes one line:
import pandas as pd data = pd.read_csv('data.csv')
Pandas loads the entire file into a structure called a DataFrame, which is like a spreadsheet inside Python. You can sort it, filter it, and do math on it very quickly. But for a straightforward task — reading a CSV and looping through the rows — the csv module is simpler and does not require you to read anything.
Frequently Asked Questions
What if my CSV file is in a different folder?
Use the full path to the file. On Windows, that might be 'C:\\Users\\YourName\\Documents\\data.csv'. On Mac or Linux, it might be '/Users/YourName/Documents/data.csv'. You can also use a relative path if the file is in the same folder as your Python script — just use the filename like 'data.csv'.
How do I know if the file opened successfully?
If the file does not exist or the path is wrong, Python will show an error message that says "FileNotFoundError". If you see that, check that the filename is spelled correctly and that the file is actually in the location you specified.
Can I read a CSV file from the internet?
Yes, but you need the requests library to read it first. You can also use pandas, which can read directly from a URL: pd.read_csv('http://example.com/data.csv'). With the csv module, you have to read the file to your computer first.
What is the difference between csv.reader and csv.DictReader?
csv.reader gives you each row as a list, so you access columns by their position number. csv.DictReader gives you each row as a dictionary, so you access columns by their name. DictReader is easier if your CSV has a header row, because you do not have to remember which column is which number.
Do I have to close the file manually?
No, not if you use the with statement. The with statement automatically closes the file when the block ends. If you open a file without with, you should close it manually by calling file.close() when you are done.