Automation
How to Read and Write CSV Files With Python
CSV files are common exports from spreadsheets and business tools. Python's `csv` module handles quoting and delimiters more reliably than splitting each line on commas.
Step by step
- Open files with `newline=''` when using csv.
- Use `DictReader` and `DictWriter` for named columns.
- Specify UTF-8 encoding for predictable text handling.
- Validate required fields before processing production data.
Run this example
Use Python 3. Copy the code into a file or interactive Python environment.
import csv
import io
source = io.StringIO("name,score\nAda,9\nLin,8\n")
rows = list(csv.DictReader(source))
output = io.StringIO(newline="")
writer = csv.DictWriter(output, fieldnames=["name", "score"], lineterminator="\n")
writer.writeheader()
writer.writerows(rows)
print(output.getvalue().strip())Expected output
name,score
Ada,9
Lin,8Common issue
A `KeyError` may mean your CSV header has a different name or a leading BOM; inspect `reader.fieldnames`.
Try it yourself
Add a `status` column and write `pass` for scores of eight or higher.
CSV is structured text
Do not split rows with split(","): quoted fields can contain commas. Use the csv module so quoting and delimiters are handled correctly. Open real CSV files with newline="" and an explicit encoding. The first row supplies column names for DictReader.
Read, validate and write a report
StringIO keeps this example self-contained. An explicit line terminator makes the printed output identical across operating systems. For a real file, replace StringIO with an open file object.
import csv
from io import StringIO
source = StringIO("item,units\nNotebook,3\nPen,2\n")
result = StringIO(newline="")
writer = csv.DictWriter(result, fieldnames=["item", "units"], lineterminator="\n")
writer.writeheader()
total = 0
for row in csv.DictReader(source):
units = int(row["units"])
if units < 0:
raise ValueError("units cannot be negative")
total += units
writer.writerow({"item": row["item"].strip(), "units": units})
print(result.getvalue(), end="")
print(f"Total units: {total}")Expected output
item,units
Notebook,3
Pen,2
Total units: 5Handle messy files deliberately
Check required columns before processing. Report row numbers for bad numeric values rather than silently turning them into zero. If a UTF-8 file has a byte-order mark, encoding="utf-8-sig" can remove it on reading. A semicolon-delimited file needs a matching delimiter.
Exercise
Add Pencil,4; the total should become 9. Try a quoted item containing a comma and confirm it remains one field. Change units to many and catch the conversion error with a message that includes the row number.
Keep learning
Read the relevant documentation · Free foundational resources
Related guides: 10 Useful Python Scripts for Beginners and How to Automate Excel Tasks With Python.
Interested in applying these skills to a product? Explore the optional Python to Profit course. It focuses on product and business building, not replacing foundational Python study.