csv.reader() / csv.writer()
Functions from the csv module for reading and writing CSV files. Use these when processing spreadsheet or tabular data.
Syntax
import csv
# Read a CSV file.
with open('data.csv', 'r', encoding='utf-8', newline='') as f:
reader = csv.reader(f)
for row in reader:
print(row) # Each row is returned as a list.
# Write to a CSV file.
with open('output.csv', 'w', encoding='utf-8', newline='') as f:
writer = csv.writer(f)
writer.writerow(['Name', 'Age', 'City'])
writer.writerows([['Tanaka', 30, 'Tokyo'], ['Sato', 25, 'Osaka']])
Function List
| Function | Description |
|---|---|
| csv.reader(f) | Takes a CSV file object and returns an iterator that yields each row as a list. |
| csv.writer(f) | Takes a CSV file object and returns a writer object for writing data in CSV format. |
| writer.writerow(row) | Writes a single row (a list) to the CSV file. |
| writer.writerows(rows) | Writes multiple rows (a list of lists) to the CSV file at once. |
| csv.DictReader(f) | Treats the first row as a header and returns an iterator that yields each row as a dictionary. |
| csv.DictWriter(f, fieldnames) | Returns a writer object for writing rows as dictionaries to a CSV file. |
| writer.writeheader() | Writes the header row (field names) when using DictWriter. |
Sample Code
import csv
# Create a CSV file using csv.writer().
rows = [
['Name', 'Age', 'City'],
['Taro Tanaka', 30, 'Tokyo'],
['Hanako Sato', 25, 'Osaka'],
['Ichiro Suzuki', 35, 'Nagoya'],
]
with open('users.csv', 'w', encoding='utf-8', newline='') as f:
writer = csv.writer(f)
writer.writerows(rows)
# Read the CSV file using csv.reader().
with open('users.csv', 'r', encoding='utf-8', newline='') as f:
reader = csv.reader(f)
header = next(reader) # Read the header row.
print(f'Header: {header}')
for row in reader:
print(row) # Each row is printed as a list.
# Write rows as dictionaries using DictWriter.
users = [
{'name': 'Yamada', 'age': 28, 'city': 'Fukuoka'},
{'name': 'Takahashi', 'age': 42, 'city': 'Sapporo'},
]
with open('users2.csv', 'w', encoding='utf-8', newline='') as f:
writer = csv.DictWriter(f, fieldnames=['name', 'age', 'city'])
writer.writeheader()
writer.writerows(users)
# Read rows as dictionaries using DictReader.
with open('users2.csv', 'r', encoding='utf-8', newline='') as f:
reader = csv.DictReader(f)
for row in reader:
print(f"{row['name']}, lives in {row['city']}")
# Prints something like: "Yamada, lives in Fukuoka"
# To write a tab-separated (TSV) file, specify the delimiter.
with open('data.tsv', 'w', encoding='utf-8', newline='') as f:
writer = csv.writer(f, delimiter='\t')
writer.writerow(['A', 'B', 'C'])
Notes
When opening a CSV file, it is important to specify newline=''. Omitting it can cause inconsistent line ending behavior across platforms — for example, double line breaks on Windows.
Using csv.DictReader lets you access each row by column name rather than index, so you don't need to worry about column order. Because both reader and writer use an iterator-based approach, they are memory-efficient even for large datasets.
CSV files created on Windows may use the Shift-JIS (cp932) encoding. Reading them with encoding="utf-8" will result in garbled text, so make sure to match the encoding to the actual file.
If you find any errors or copyright issues, please contact us.