CSV (Comma Separated Values) is a simple and widely used format to store tabular data such as spreadsheets, database exports, reports, or logs. Python provides built-in support for reading and writing CSV files through the csv module. The module helps parse CSV files correctly, handle delimiters, manage quoting, and work with both simple and complex datasets.
CSV files are easy to create and use, making them a common choice in data processing, analysis, machine learning, and automation tasks.
The csv module in Python offers tools to read from and write to CSV files. It supports:
The module focuses on correct CSV parsing rather than plain string splitting.
The csv.reader function reads data line-by-line and returns each row as a list of values.
Name,Age,City
Aarav,21,Delhi
Meera,19,Mumbai
import csv
with open("data.csv") as f:
reader = csv.reader(f)
for row in reader:
print(row)
Output:
['Name', 'Age', 'City']
['Aarav', '21', 'Delhi']
['Meera', '19', 'Mumbai']
Each row becomes a Python list.
Often we want to skip the first row (header).
import csv
with open("data.csv") as f:
reader = csv.reader(f)
next(reader)
for row in reader:
print(row)
Output:
['Aarav', '21', 'Delhi']
['Meera', '19', 'Mumbai']
DictReader maps each row to a dictionary using the header names.
import csv
with open("data.csv") as f:
reader = csv.DictReader(f)
for row in reader:
print(row)
Output:
{'Name': 'Aarav', 'Age': '21', 'City': 'Delhi'}
{'Name': 'Meera', 'Age': '19', 'City': 'Mumbai'}
Using a dictionary avoids index-based access and improves readability.
The csv.writer is used to write rows to a CSV file.
import csv
with open("output.csv", "w", newline="") as f:
writer = csv.writer(f)
writer.writerow(["Name", "Age", "City"])
writer.writerow(["Rahul", 22, "Chennai"])
writer.writerow(["Neha", 20, "Kolkata"])
Output in output.csv:
Name,Age,City
Rahul,22,Chennai
Neha,20,Kolkata
newline="" prevents blank lines on Windows.
DictWriter writes rows using dictionary keys.
import csv
with open("students.csv", "w", newline="") as f:
fieldnames = ["Name", "Age", "City"]
writer = csv.DictWriter(f, fieldnames=fieldnames)writer.writeheader()
writer.writerow({"Name": "Rohan", "Age": 23, "City": "Pune"})
writer.writerow({"Name": "Sara", "Age": 21, "City": "Bangalore"})
Output:
Name,Age,City
Rohan,23,Pune
Sara,21,Bangalore
DictWriter makes the code cleaner when working with structured data.
CSV files do not always use commas. Some use semicolons, pipes, or tabs.
import csv
with open("semicolon.csv") as f:
reader = csv.reader(f, delimiter=";")
for row in reader:
print(row)
import csv
with open("pipe.csv", "w", newline="") as f:
writer = csv.writer(f, delimiter="|")
writer.writerow(["A", "B", "C"])
CSV files may contain commas in the data. In those cases, values must be quoted.
Name,Comment
Aarav,"Hello, how are you?"
Python handles this correctly using quoting controls.
import csv
with open("comments.csv") as f:
reader = csv.reader(f)
for row in reader:
print(row)
CSV files containing millions of rows should be processed line-by-line to avoid memory issues.
import csv
with open("large.csv") as f:
reader = csv.reader(f)
for row in reader:
pass
The csv module streams data and does not load the whole file at once.
import csv
with open("data.csv") as f:
rows = list(csv.reader(f))
print(rows)
import csv, json
with open("data.csv") as f:
reader = csv.DictReader(f)
data = list(reader)with open("data.json", "w") as f:
json.dump(data, f, indent=4)
import csv
try:
with open("unknown.csv") as f:
reader = csv.reader(f)
for row in reader:
print(row)
except FileNotFoundError:
print("File not found")
| Function | Purpose |
|---|---|
| csv.reader() | Read CSV rows as lists |
| csv.DictReader() | Read CSV rows as dictionaries |
| csv.writer() | Write CSV rows as lists |
| csv.DictWriter() | Write CSV rows as dictionaries |
| delimiter= | Change CSV delimiter |
| writerow() | Write a single row |
| writerows() | Write multiple rows |
| writeheader() | Write column names |