Day 6: Files, CSV, JSON, Errors & Modules
✅ Today you will learn
- Read and write text files safely with
with open - Process CSV files with the csv module
- Read and write JSON – the language of APIs
- Handle bad data with try / except / finally
- Use modules: math, statistics, datetime, random, os
🏢 In the real world
Every night a shop exports the day’s bills as a CSV, and the payment gateway sends a JSON file. A small Python script that reads both, skips broken rows instead of crashing and writes a summary is a very common first task for junior analysts.
Text files
with open("notes.txt", "w", encoding="utf-8") as f: # w = write (new file)
f.write("Day 6 – files and errors\n")
f.write("Data science needs clean files.\n")
with open("notes.txt", "a", encoding="utf-8") as f: # a = append
f.write("Line added later.\n")
with open("notes.txt", encoding="utf-8") as f: # default r = read
for n, line in enumerate(f, 1):
print(n, line.strip())with open(...) closes the file automatically, even if an error happens. Always add encoding="utf-8" so Hindi and ₹ symbols are saved correctly.
CSV files with the csv module
import csv
rows = [["bill_no", "item", "qty", "price"],
[1, "Tea 1kg", 2, 520], [2, "Atta 10kg", 1, 459], [3, "Earbuds", 1, "1,499"],
[4, "Pen Set", "two", 150], [5, "Bedsheet", 3, 699]]
with open("bills.csv", "w", newline="", encoding="utf-8") as f:
csv.writer(f).writerows(rows)
print(open("bills.csv", encoding="utf-8").read())import csv
total, bad = 0, []
with open("bills.csv", encoding="utf-8") as f:
for row in csv.DictReader(f): # each row is a dict
try:
qty = int(row["qty"])
price = float(row["price"].replace(",", ""))
total += qty * price
except ValueError:
bad.append(row["bill_no"])
print("Total sales: ₹", total)
print("Rows skipped (bad data):", bad)JSON – data from APIs
import json
payments = [{"txn": "T1", "mode": "UPI", "amount": 520.0, "ok": True},
{"txn": "T2", "mode": "Card", "amount": 1499.0, "ok": False},
{"txn": "T3", "mode": "UPI", "amount": 459.0, "ok": True}]
with open("payments.json", "w", encoding="utf-8") as f:
json.dump(payments, f, indent=2)
with open("payments.json", encoding="utf-8") as f:
data = json.load(f)
success = [p for p in data if p["ok"]]
print("Successful:", len(success), "| Amount: ₹", sum(p["amount"] for p in success))
print(json.dumps(data[1])) # dict -> JSON text| Python | JSON |
|---|---|
| dict | object { } |
| list | array [ ] |
| str | string |
| int / float | number |
| True / False | true / false |
| None | null |
Errors and exceptions
def safe_average(values):
try:
avg = sum(values) / len(values)
except ZeroDivisionError:
print("No values – returning 0")
return 0
except TypeError as e:
print("Bad value in list:", e)
return None
else:
return round(avg, 2) # runs only if no error
finally:
print("checked", values) # always runs
print(safe_average([10, 20, 35]))
print(safe_average([]))
print(safe_average([10, "x"]))def check_age(age):
if not 0 < age < 120:
raise ValueError(f"Impossible age: {age}")
return age
for a in [25, 150]:
try:
print("OK", check_age(a))
except ValueError as e:
print("Rejected ->", e)Useful built-in modules
import math, statistics as st, random
from datetime import date, datetime, timedelta
print(math.sqrt(144), math.ceil(4.2), math.floor(4.8), round(math.pi, 4))
marks = [56, 78, 91, 45, 88, 67, 78]
print("mean", round(st.mean(marks), 2), "| median", st.median(marks), "| mode", st.mode(marks))
random.seed(1) # same "random" result every run
print("Lucky draw:", random.choice(["Aman", "Priya", "Rahul", "Sneha"]), random.sample(range(1, 50), 3))
d = date(2024, 10, 31)
print(d.strftime("%d %b %Y, %A"), "| +45 days:", d + timedelta(days=45))
print(datetime.strptime("15/08/2024", "%d/%m/%Y").year)import os
print("Current folder has CSV files:", sorted(f for f in os.listdir(".") if f.endswith(".csv"))[:4])
print("bills.csv exists:", os.path.exists("bills.csv"), "| size:", os.path.getsize("bills.csv"), "bytes")
os.makedirs("reports", exist_ok=True)
print(os.path.join("reports", "summary.txt"))Your own module
code = 'def gst(amount, rate=18):\n return round(amount * rate / 100, 2)\n'
with open("mytools.py", "w") as f: # normally you create this file in an editor
f.write(code)
import mytools
print(mytools.gst(1499), mytools.gst(459, rate=5))Real example: nightly sales report
import csv, json
from datetime import date
summary = {"date": str(date(2024, 11, 1)), "bills": 0, "revenue": 0.0, "skipped": 0}
with open("bills.csv", encoding="utf-8") as f:
for row in csv.DictReader(f):
try:
summary["revenue"] += int(row["qty"]) * float(row["price"].replace(",", ""))
summary["bills"] += 1
except ValueError:
summary["skipped"] += 1
with open("reports/daily_summary.json", "w") as f:
json.dump(summary, f, indent=2)
print(open("reports/daily_summary.json").read())🏅 Job skills unlocked today
Practice tasks
🏋️ Practice – do it yourself
marks.csv with name and marks for 5 students, then read it back and print the class average.Show solution
import csv
data = [["name", "marks"], ["Aman", 78], ["Priya", 95], ["Rahul", 52], ["Sneha", 81], ["Kabir", 67]]
with open("marks.csv", "w", newline="") as f:
csv.writer(f).writerows(data)
with open("marks.csv") as f:
m = [int(r["marks"]) for r in csv.DictReader(f)]
print("Average:", sum(m) / len(m))Show solution
import json
text = '[{"name": "Pen", "price": 20}, {"name": "Bag", "price": 899}, {"name": "Eraser", "price": 5}]'
items = json.loads(text)
print(min(items, key=lambda p: p["price"])["name"])Show solution
from datetime import date print((date(2025, 3, 31) - date(2025, 1, 1)).days, "days")
Quick quiz
🧠 Quick Quiz – test yourself
Interview corner
💼 Interview Corner
How would you process a 5 GB CSV that does not fit in memory?
Read it in chunks – line by line with the csv module, or pd.read_csv(..., chunksize=100000) in pandas – aggregate each chunk and combine the results. Only load the columns you need.
Why catch specific exceptions instead of a bare except?
A bare except hides real bugs (even typos and KeyboardInterrupt). Catching ValueError or KeyError handles only the expected bad data and lets other problems surface.
Summary
- with open(..., encoding="utf-8") reads and writes files safely.
- csv.DictReader + try/except processes messy CSVs without crashing.
- json.dump / json.load move data to and from APIs and files.
- Modules like statistics, datetime and os save you writing code.
Day 7: put the whole week together in a mini project – a personal expense analyzer in pure Python.
