§ 2.10section 18 of 18·~6 min
Mini project: clean a messy orders file
Time to put it together. A vendor sends you orders.csv. Clean it and write orders_clean.json. This is a tiny ETL job: extract, transform, load.
| id | amount | city |
|---|---|---|
"1" | " 499 " | "pune" |
"2" | "N/A" | "Delhi" |
"1" | " 499 " | "pune" |
"3" | "1299" | " Mumbai" |
- Read the CSV into a list of dicts.
- Trim the city and fix its case.
- Convert amount to an
int. Send rows that fail torejects. - Drop rows whose id you've already seen.
- Write the clean rows to JSON and print a summary.
import csv, json
def clean_row(row):
return {
"id": int(row["id"]),
"amount": int(row["amount"].strip()),
"city": row["city"].strip().title(),
}
clean, rejects, seen = [], [], set()
with open("orders.csv") as f:
for row in csv.DictReader(f):
try:
r = clean_row(row)
except ValueError:
rejects.append(row)
continue
if r["id"] in seen:
continue
seen.add(r["id"])
clean.append(r)
with open("orders_clean.json", "w") as f:
json.dump(clean, f, indent=2)
print(f"{len(clean)} clean, {len(rejects)} rejected")
OUTPUT
2 clean, 1 rejectedon the job This shape (read, clean each row, quarantine failures, dedupe, write) is the same one you'll build later with pandas, Spark and Airflow. Only the tools change.
LABDE-142· hands-on
Orders double-counted
Fix a real ticket in a small codebase you didn't write: read the code, change only the functions you're given, pass the tests, then submit for hidden checks and a score.
Open the lab →