How to Read and Parse a CSV File in Python
Learn how to read and parse CSV files in Python using the built-in csv module. Master reading as lists, dictionary readers, and custom delimiters.
Explanation
CSV (Comma-Separated Values) is a ubiquitous file format for storing tabular datasets. Whether importing databases, exporting spreadsheets, or downloading logs, CSV reading is a core skill. Python includes a dedicated, highly optimized built-in `csv` module to parse CSV files easily without needing third-party libraries like pandas.
The `csv.reader` function reads a file line-by-line and parses it, splitting each row by the comma delimiter and returning lists of strings. If your CSV file has a header row (e.g., column labels), using `csv.DictReader` is often preferred. It automatically parses each row into a dictionary where the keys are the column labels, making code much more readable and robust to changes in column order.
When opening CSV files, it is a best practice to set `newline=""` in the `open()` statement. This prevents Python from doing automated newline translations, which can lead to parsing errors on certain operating systems. Additionally, always specify `encoding="utf-8"` to handle special characters.
Step-by-Step Implementation
- 1
Import the built-in csv module in your script.
- 2
Open the CSV file using with open("employees.csv", "r", newline="", encoding="utf-8") as file.
- 3
Pass the file object to csv.reader(file) to parse the file rows as lists of strings.
- 4
Use csv.DictReader(file) to parse rows as dictionary mappings corresponding to column headers.
Code Example
This script demonstrates writing a sample CSV file, and reading it using both csv.reader and csv.DictReader.
import csv
# Writing a sample CSV file
sample_content = [
["name", "role", "salary"],
["Alice", "Developer", "90000"],
["Bob", "Manager", "110000"]
]
with open("employees.csv", "w", newline="", encoding="utf-8") as f:
writer = csv.writer(f)
writer.writerows(sample_content)
# 1. Reading CSV using csv.reader (List representation)
print("--- Using csv.reader ---")
with open("employees.csv", "r", newline="", encoding="utf-8") as file:
reader = csv.reader(file)
header = next(reader) # Skip header row
print("Header:", header)
for row in reader:
print(f"Employee: {row[0]} | Role: {row[1]}")
# 2. Reading CSV using csv.DictReader (Dictionary representation)
print("\n--- Using csv.DictReader ---")
with open("employees.csv", "r", newline="", encoding="utf-8") as file:
dict_reader = csv.DictReader(file)
for row in dict_reader:
print(f"Name: {row['name']} | Role: {row['role']} | Salary: {row['salary']}")--- Using csv.reader ---
Header: ['name', 'role', 'salary']
Employee: Alice | Role: Developer
Employee: Bob | Role: Manager
--- Using csv.DictReader ---
Name: Alice | Role: Developer | Salary: 90000
Name: Bob | Role: Manager | Salary: 110000Frequently Asked Questions
Why should I set newline="" when opening a CSV file?
Setting newline="" prevents Python from modifying end-of-line characters dynamically, ensuring the csv module can parse csv-style newlines correctly across different operating systems.
How do I parse a CSV file that uses tabs or semicolons instead of commas?
You can pass a custom delimiter to the reader function: csv.reader(file, delimiter="\t") or csv.reader(file, delimiter=";").
Related How-To Guides
Recommended Python Resources
Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.
Python File I/O
Learn how to read and write files in Python. Master open(), read(), write(), and context managers to handle file operations securely without resource leaks.
Python File Operations
Quick reference for reading and writing files in Python. Master open modes, context managers, and reading line-by-line.
Python vs JavaScript: Which Programming Language is Best?
A comprehensive comparison between Python and JavaScript. Explore syntax differences, performance, use cases (backend vs frontend), and coding examples.