Two Idiomatic Options
Python offers two dominant paths: the standard library csv module (built-in, zero dependencies) and pandas (batteries-included analytics).
Both handle quoting and delimiter correctly when you use them properly. Both can go wrong if you skip encoding or force numeric types on ID columns.
| Choice | Best for |
|---|---|
| csv module | Small scripts, CLI tools, no dependencies |
| pandas | Analytics, cleanup, joins, type inference |
| polars/pyarrow | Very large files, columnar performance |
Step-by-Step: Read CSV with the csv Module
Use DictReader for header-based access. It is the most readable option.
import csv
with open("orders.csv", newline="", encoding="utf-8") as f:
reader = csv.DictReader(f)
for row in reader:
print(row["order_id"], row["email"])Semicolon or tab delimiter
Pass the actual separator instead of guessing.
reader = csv.DictReader(f, delimiter=";")
# or delimiter="\t" for TSVWriting CSV back
DictWriter mirrors DictReader.
with open("out.csv", "w", newline="", encoding="utf-8") as f:
writer = csv.DictWriter(f, fieldnames=["order_id", "email"])
writer.writeheader()
writer.writerows(rows)Step-by-Step: Read CSV with pandas
pandas is worth adding whenever you need cleanup or analysis beyond one loop.
import pandas as pd
df = pd.read_csv("orders.csv", dtype={"order_id": str}, encoding="utf-8")
print(df.head())
print(df.dtypes)Force text for IDs
Use dtype to keep leading zeros and long numeric strings intact. Numbers-as-text prevents scientific notation surprises.
Parse dates
Prefer parse_dates for known date columns. Ambiguous locale dates should be normalized upstream when possible.
df = pd.read_csv("orders.csv", parse_dates=["order_date"])Read in chunks for large files
Iterate DataFrames without loading everything at once.
for chunk in pd.read_csv("big.csv", chunksize=100_000, dtype=str):
process(chunk)Delimiter, Encoding, and Nulls
Most "broken CSV" issues in Python trace back to these three settings.
| Setting | Guidance |
|---|---|
| Delimiter | Set explicitly (",", ";", "\t") |
| Encoding | utf-8 by default; try windows-1252 for legacy files |
| Nulls | Decide how empty strings map (NaN vs "") |
| Quoting | Rely on csv/pandas parsers, not manual splitting |
Common Mistakes
Skip these to save hours.
- Using open() without newline="" (breaks multiline quoted fields).
- Splitting strings on commas manually.
- Ignoring encoding and hitting UnicodeDecodeError.
- Letting pandas auto-type ID columns and losing leading zeros.
- Loading a huge file into memory when chunking would work.
Best Practices
Idiomatic Python + a bit of hygiene.
- Always specify encoding.
- Use DictReader/DictWriter for readability.
- Type IDs as str in pandas.
- Parse dates explicitly.
- Chunk large files.
- Validate row shape and required columns.
Real-World Examples
Two patterns cover most jobs.
One-off CLI clean and export
A Python script normalizes headers, trims values, and writes a cleaned CSV for import.
Analytics ETL
pandas reads, filters, joins, and writes back to CSV before uploading to a warehouse.
Why Use Convert CSV Online?
Sometimes you want a preview before writing more code. Convert CSV Online is free, browser-based, and requires no account for everyday conversions. Preview a file in the Online CSV Editor, then run your Python against a CSV whose structure and encoding you already trust. Client-side workflows work on Windows, macOS, and Linux browsers.
Verify before you script
Open the file online, confirm delimiter, encoding, and headers, then wire up csv/pandas with confidence.
Conclusion
Python parses CSV cleanly when you pick the right tool and set delimiter and encoding explicitly. csv for small scripts, pandas for analysis, chunks for scale.
FAQ
How do I parse a CSV file in Python?
Use the built-in csv module (csv.DictReader) for simple scripts, or pandas.read_csv for analytics workflows. Set encoding and delimiter explicitly.
Should I use csv or pandas?
Use csv for small scripts and low dependencies. Use pandas when you need cleanup, joins, or type-aware analysis.
How do I keep leading zeros in pandas?
Pass dtype={"column": str} to pandas.read_csv or read all columns as strings with dtype=str.
How do I read a large CSV in Python?
Use pandas chunksize to iterate DataFrame chunks, or stream through csv.reader without loading everything.
Why do I get UnicodeDecodeError?
The file is not UTF-8. Try encoding="windows-1252" or another legacy code page, then re-save as UTF-8.
How do I write CSV in Python?
Use csv.DictWriter with newline="" and encoding="utf-8". For pandas, use df.to_csv(path, index=False).
References
Convert your CSV in the browser
Preview, clean, and convert CSV files free with Convert CSV Online—no installation and no account required for everyday conversions.