What Is a CSV File?
CSV stands for Comma-Separated Values. It's a plain-text file where each row is a record and commas separate the columns. Almost every spreadsheet app (Excel, Google Sheets) can export to CSV.
name,score,grade
Alex,95,A
Jordan,88,B
Taylor,92,A
💡
First Row = Headers
The first row of a CSV usually contains column names (headers). csv.DictReader uses these headers as dictionary keys automatically.
Reading CSV with Python
Python's built-in csv module makes reading CSV files easy. Use csv.DictReader to get each row as a dictionary:
import csv
with open("scores.csv") as f:
reader = csv.DictReader(f)
for row in reader:
name = row["name"]
score = int(row["score"])
print(name, "scored", score)
Processing CSV Columns
Once data is loaded, you can apply list and math operations to any column:
scores = []
with open("scores.csv") as f:
for row in csv.DictReader(f):
scores.append(int(row["score"]))
print("Average:", sum(scores) / len(scores))
print("Highest:", max(scores))
🆕
Real Datasets
Kaggle.com has thousands of free CSV datasets — weather, sports, movies, and more. Once you know csv.DictReader, you can analyze any of them!