๐ Tutorial ยท 6 min read
How to Open a CSV File in Python (csv Module & pandas)
To open a CSV in Python, use the built-in csv module for a plain read, or pandas.read_csv() when you plan to analyse the data. Both handle the parts that break a naive split โ quoted fields, commas inside values, and line breaks inside a record. The two things that actually trip people up are the delimiter and the encoding, and each is one argument away.
The quick answer
Standard library, no installs:
import csv
with open("data.csv", newline="", encoding="utf-8") as f:
for row in csv.reader(f):
print(row)
Or with pandas, if the file is going to become a table you filter and summarise:
import pandas as pd
df = pd.read_csv("data.csv")
print(df.head())
print(df.shape) # (rows, columns)
The newline="" in the first example is not decoration. The csv module needs to see the file's line breaks itself, so the standard library docs tell you to open the file with newline="" and let the parser split records.
Why not just split the text yourself?
Because a CSV is not a list of values separated by commas โ it is a format described by RFC 4180, where any field may be wrapped in double quotes and may contain a comma or even a line break. Splitting on the comma gets those fields wrong. Here is the same three-record file read two ways.
A naive field split fragments the record wherever a comma sits inside quotes:
$ awk -F, '{print NF" fields: "$1" | "$2}' t.csv
3 fields: name | city
4 fields: "Doe | Jane"
1 fields: and commas" |
3 fields: Bob | Tokyo
The csv module returns the three records with the quoted comma kept intact:
import csv
for row in csv.reader(open("t.csv", newline="", encoding="utf-8")):
print(row)
# ['name', 'city', 'note']
# ['Doe, Jane', 'Paris', 'likes "quotes"\nand commas']
# ['Bob', 'Tokyo', 'plain']
That output is from Python 3.13.5 on a file whose second field is "Doe, Jane" and whose third spans two physical lines. The naive split produced four fragments; the real parser produced three rows. This is the single best reason to use a CSV parser instead of split(",") in any language.
Open a CSV as dictionaries with csv.DictReader
If your file has a header row and you would rather address columns by name than by index, use csv.DictReader. It turns each record into a dictionary keyed by the header, which makes the rest of the code readable:
import csv
with open("data.csv", newline="", encoding="utf-8") as f:
for row in csv.DictReader(f):
print(row["name"], "->", row["city"])
Read only the columns you need with pandas
On a wide CSV, loading every column wastes time and memory. usecols restricts the read, and dtype stops pandas from guessing a type you did not want:
df = pd.read_csv("data.csv", usecols=["name", "city", "signup_date"])
Type guessing is worth watching: pandas will turn a column of ZIP codes or part numbers into integers and drop the leading zeros, and it will read a numeric-looking ID as a number unless you tell it otherwise. Pass dtype={"zip": str} for those columns. The same class of problem in Excel is covered in keep leading zeros in CSV.
Read a file that is too big to fit in memory
Do not hand a multi-gigabyte CSV to a single read_csv() call. Two habits keep memory flat:
- csv module: iterate
csv.reader. It is a generator over the file, so only the current row is held at a time. - pandas: pass
chunksizeand loop over the iterator:
for chunk in pd.read_csv("huge.csv", chunksize=100_000):
process(chunk) # each chunk is a DataFrame of 100k rows
If the job is really about size rather than code, the file may be beyond what a spreadsheet can even open โ Excel stops at 1,048,576 rows and 16,384 columns per sheet, per Microsoft's documented limits. See a CSV too big for Excel and why a CSV is too large to analyse as-is.
The two gotchas: delimiter and encoding
Encoding. open() uses your platform's default encoding, which is why a UTF-8 file read on a Windows machine using cp1252 can raise UnicodeDecodeError or produce symbols. Be explicit: pass encoding="utf-8", and utf-8-sig if the file begins with a byte-order mark. If the file came from an older Windows export, cp1252 is the other common value. pandas takes the same argument in read_csv(), and mis-decoded text looks identical to the garbled-file symptom described in fix garbled CSV.
Delimiter. European exports often use semicolons, and TSV files use tabs. Pass delimiter=";" or delimiter="\t" to csv.reader, or sep=";" to read_csv(). You can also let csv.Sniffer guess, but a guess is still a guess โ when the columns land wrong, set it by hand. The wider problem is covered in change a CSV delimiter.
If you would rather not write code
Reading a CSV in Python is the right tool when the file feeds a script. For a one-off look โ confirm the columns, count the rows, check a few values โ a browser-based viewer is faster and needs nothing installed. Drop the file into the free CSV analyzer and it renders as a table on your device, with no upload. To turn the same data into JSON instead, use the JSON โ CSV converter. To run SQL against a CSV without a database, see query a CSV with SQL.
Frequently Asked Questions
How do I open a CSV file in Python?
Use the built-in csv module for a plain read: import csv, then open the file with newline="" and pass it to csv.reader or csv.DictReader. If you plan to analyse the data, pandas.read_csv("file.csv") loads it into a DataFrame in one line. Both parse quoted commas and embedded line breaks correctly.
Should I use the csv module or pandas?
Use csv when you only need to stream rows and want no dependencies โ it ships with Python. Use pandas when you need filtering, grouping or statistics, because it gives you a DataFrame; it is a third-party library you install separately.
Why does my CSV look garbled or raise UnicodeDecodeError?
It is an encoding mismatch. open() defaults to the system encoding, so a UTF-8 file read as cp1252 fails, and a cp1252 file read as UTF-8 shows mangled characters. Pass encoding="utf-8" explicitly, and use "utf-8-sig" when the file starts with a byte-order mark.
How do I read a CSV that is too big for memory?
Do not load it whole. With csv, iterate csv.reader so only one row is held at a time. With pandas, pass chunksize to read_csv and loop over the chunks, or use usecols to read only the columns you need.
NoCodeCSV Team
Updated October 8, 2026 ยท Practical guides by the NoCodeCSV team.
Go further with AI data tools
NoCodeCSV handles the basics for free. When your data work grows, these tools pair well with it:
- Stack AI โ build AI workflows that process your CSVs automatically, end to end. Try Stack AI
- Softr โ turn your cleaned data into customer-facing apps and portals without code. Try Softr
- Toggl Track โ track time spent on data projects and client work. Try Toggl
Some links above are affiliate links โ if you buy through them we may earn a commission at no extra cost to you.
Read Your CSV Without Writing Code
Upload any CSV and see it as a clean table in seconds. No signup needed.
Related reading
CSV Fundamentals โ other guides that pair well with this one.
- What Is CSV File Format? (RFC 4180 Explained)
- CSV vs Excel
- How to Read a CSV in JavaScript
- How to Open a CSV in R
Browse all guides in the NoCodeCSV blog.