📊 Working with CSV¶
CSV (Comma-Separated Values) is a simple format for tabular data.
Python's built-in csv module handles quoting and parsing correctly.
✅ Reading CSV¶
import csv
with open("people.csv", "r", encoding="utf-8", newline="") as f:
reader = csv.DictReader(f)
rows = list(reader)
✅ Writing CSV¶
import csv
rows = [{"name": "Ada", "age": "30"}]
with open("people.csv", "w", encoding="utf-8", newline="") as f:
writer = csv.DictWriter(f, fieldnames=["name", "age"])
writer.writeheader()
writer.writerows(rows)
⚠️ Why newline=""?¶
On Windows, not using newline="" can lead to extra blank lines in CSV output.
🔍 Key Takeaways¶
- Use
csv.readerfor plain lists. - Use
csv.DictReader/DictWriterfor headers + dict rows. - Open CSV files with
newline="".