CSV Files: Structured Data in Plain Text
The idea
A CSV file is a text file where each line is a row and each value is separated by a comma.
That's it. No special format, no proprietary software. Just commas and newlines.
name,score,active
Raven,85,True
Wolf,74,False
Ghost,91,True
Open it in Excel — you get a spreadsheet. Open it in Python — you get data you can work with.
CSV is the most common format for structured data at beginner level. If you'll ever deal with real-world data, you'll deal with CSV.
The file for this lesson
Download soldiers.csv and place it in the same folder as your script before running the examples.
Reading a CSV file — with csv.reader
Python has a built-in csv module. Import it, open the file, wrap it in csv.reader:
import csv
with open("soldiers.csv", "r", encoding="utf-8") as f:
reader = csv.reader(f)
for row in reader:
print(row)
Output:
['name', 'score', 'active']
['Raven', '85', 'True']
['Wolf', '74', 'False']
['Ghost', '91', 'True']
Each row is a list. Every value is a string — including numbers. Convert when needed.
Skipping the header row
import csv
with open("soldiers.csv", "r", encoding="utf-8") as f:
reader = csv.reader(f)
next(reader) # skip header
for row in reader:
name, score, active = row
print(f"{name}: {int(score)}")
Output:
Raven: 85
Wolf: 74
Ghost: 91
Reading into a dict — csv.DictReader
DictReader uses the header row as keys — each row becomes a dict:
import csv
with open("soldiers.csv", "r", encoding="utf-8") as f:
reader = csv.DictReader(f)
for row in reader:
print(row["name"], row["score"])
Output:
Raven 85
Wolf 74
Ghost 91
Cleaner than unpacking by position. Use this when your CSV has headers.
Writing a CSV file — csv.writer
import csv
soldiers = [
["name", "score", "active"],
["Raven", 85, True],
["Wolf", 74, False],
["Ghost", 91, True]
]
with open("output.csv", "w", encoding="utf-8", newline="") as f:
writer = csv.writer(f)
writer.writerows(soldiers)
Pass newline="" when writing CSV on Windows — it prevents double newlines.
writerows() writes a list of rows. writerow() writes one row at a time.
Writing from a list of dicts — csv.DictWriter
import csv
soldiers = [
{"name": "Raven", "score": 85, "active": True},
{"name": "Wolf", "score": 74, "active": False},
{"name": "Ghost", "score": 91, "active": True}
]
with open("output.csv", "w", encoding="utf-8", newline="") as f:
writer = csv.DictWriter(f, fieldnames=["name", "score", "active"])
writer.writeheader()
writer.writerows(soldiers)
The header is written automatically. Each dict becomes a row.
Heads up!
- All values read from CSV are strings — convert with
int(),float(), orbool()as needed - Always pass
newline=""when writing CSV files - Use
DictReaderandDictWriterwhen your file has headers — cleaner and safer - The
csvmodule is built-in — no installation needed
What you should understand now
- CSV is plain text — commas separate values, newlines separate rows
csv.readerreads rows as listscsv.DictReaderreads rows as dicts using the headercsv.writerwrites lists as rowscsv.DictWriterwrites dicts as rows- All values from CSV are strings — convert when needed