Sales Report Generator
From raw CSV export to an executive report: parse sales data, compute revenue by product and month, find the winners, and generate the summary a manager actually reads.
How it works in the real world
This is the daily reality of data analysts and the core of every BI tool (Tableau, Power BI, Excel pivot tables):
- Extract — parse the CSV export (with the csv module — never by hand).
- Transform — convert types (CSV is all strings!) and aggregate revenue by product and by month.
- Analyse — rank: top product, best month.
- Load/Report — output the formatted summary.
Data engineers call this pipeline ETL. You're about to build one end-to-end.
Extract: parse the CSV
Parse raw with csv.DictReader, then print the number of rows and the first row's product: 6 then laptop.
print(len(rows)) then print(rows[0]["product"]).
import csv, io raw = """month,product,units,price Jan,laptop,3,900 Jan,mouse,10,25 Feb,laptop,2,900 Feb,keyboard,5,60 Mar,laptop,4,900 Mar,mouse,8,25""" rows = list(csv.DictReader(io.StringIO(raw))) print(len(rows)) print(rows[0]["product"])
Run your code to check this step…
Transform: revenue per product
Revenue for a row is units × price (convert to int!). Total it per product and print each as product: revenue in first-seen order.
rev = int(row["units"]) * int(row["price"]); by_product[p] = by_product.get(p, 0) + rev.
import csv, io
raw = """month,product,units,price
Jan,laptop,3,900
Jan,mouse,10,25
Feb,laptop,2,900
Feb,keyboard,5,60
Mar,laptop,4,900
Mar,mouse,8,25"""
rows = list(csv.DictReader(io.StringIO(raw)))
by_product = {}
for row in rows:
rev = int(row["units"]) * int(row["price"])
by_product[row["product"]] = by_product.get(row["product"], 0) + rev
for product, rev in by_product.items():
print(f"{product}: {rev}")
Run your code to check this step…
Analyse: revenue per month
Aggregate revenue per month, print each as month: revenue, then Best month: Mar.
Same .get() pattern keyed on row["month"]; best = max(by_month, key=by_month.get).
import csv, io
raw = """month,product,units,price
Jan,laptop,3,900
Jan,mouse,10,25
Feb,laptop,2,900
Feb,keyboard,5,60
Mar,laptop,4,900
Mar,mouse,8,25"""
rows = list(csv.DictReader(io.StringIO(raw)))
by_month = {}
for row in rows:
rev = int(row["units"]) * int(row["price"])
by_month[row["month"]] = by_month.get(row["month"], 0) + rev
for month, rev in by_month.items():
print(f"{month}: {rev}")
print(f"Best month: {max(by_month, key=by_month.get)}")
Run your code to check this step…
Report: the executive summary
Combine everything into the final report — exactly:=== Q1 Sales Report ===Total revenue: 8850Top product: laptop (8100)Best month: Mar (3800)
top = max(by_product, key=by_product.get) — then f"Top product: {top} ({by_product[top]})".
import csv, io
raw = """month,product,units,price
Jan,laptop,3,900
Jan,mouse,10,25
Feb,laptop,2,900
Feb,keyboard,5,60
Mar,laptop,4,900
Mar,mouse,8,25"""
rows = list(csv.DictReader(io.StringIO(raw)))
by_product = {}
by_month = {}
for row in rows:
rev = int(row["units"]) * int(row["price"])
by_product[row["product"]] = by_product.get(row["product"], 0) + rev
by_month[row["month"]] = by_month.get(row["month"], 0) + rev
top = max(by_product, key=by_product.get)
best = max(by_month, key=by_month.get)
print("=== Q1 Sales Report ===")
print(f"Total revenue: {sum(by_product.values())}")
print(f"Top product: {top} ({by_product[top]})")
print(f"Best month: {best} ({by_month[best]})")
Run your code to check this step…
Project shipped!
You just built a complete, working sales report generator — the same architecture running in production software. Pick your next build →