Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🐍 Week 1 – Python for Data Science  ·  Day 06

Day 6: Files, CSV, JSON, Errors & Modules

⏱ 2 hours📶 Beginner🎯 100% self-study
Day 6 / 60

✅ 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

Python
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())
1 Day 6 – files and errors 2 Data science needs clean files. 3 Line added later.
💡 Why “with”?

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

Python
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())
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
Python
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)
Total sales: ₹ 5095.0 Rows skipped (bad data): ['4']

JSON – data from APIs

Python
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
Successful: 2 | Amount: ₹ 979.0 {"txn": "T2", "mode": "Card", "amount": 1499.0, "ok": false}
PythonJSON
dictobject { }
listarray [ ]
strstring
int / floatnumber
True / Falsetrue / false
Nonenull

Errors and exceptions

🔤
ValueErrorint("two") – wrong value
➗
ZeroDivisionErroraverage of an empty list
🔑
KeyErrorrow["phone"] when the column is missing
📁
FileNotFoundErrorwrong path or name
Errors you will see most in data work
Python
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"]))
checked [10, 20, 35] 21.67 No values – returning 0 checked [] 0 Bad value in list: unsupported operand type(s) for +: 'int' and 'str' checked [10, 'x'] None
Python
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)
OK 25 Rejected -> Impossible age: 150

Useful built-in modules

Python
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)
12.0 5 4 3.1416 mean 71.86 | median 78 | mode 78 Lucky draw: Priya [37, 49, 5] 31 Oct 2024, Thursday | +45 days: 2024-12-15 2024
Python
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"))
Current folder has CSV files: ['ab_test.csv', 'bills.csv', 'delhi_weather.csv', 'departments.csv'] bills.csv exists: True | size: 118 bytes reports/summary.txt

Your own module

Python
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))
269.82 22.95

Real example: nightly sales report

Python
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())
{ "date": "2024-11-01", "bills": 4, "revenue": 5095.0, "skipped": 1 }

🏅 Job skills unlocked today

✔ Read and write text, CSV and JSON✔ Skip bad rows instead of crashing✔ Raise clear errors for impossible data✔ Use math, statistics, datetime, random and os

Practice tasks

🏋️ Practice – do it yourself

Task 1: Write a CSV marks.csv with name and marks for 5 students, then read it back and print the class average.
Show solution
Python
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))
Average: 74.6
Task 2: Given a JSON string of three products with prices, print the cheapest product name.
Show solution
Python
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"])
Eraser
Task 3: How many days are left from 1 Jan 2025 to 31 Mar 2025 (financial-year end)? Use datetime.
Show solution
Python
from datetime import date
print((date(2025, 3, 31) - date(2025, 1, 1)).days, "days")
89 days

Quick quiz

🧠 Quick Quiz – test yourself

1. Which mode adds text to the end of an existing file?
"a" means append; "w" would erase the file first.
2. csv.DictReader gives each row as a…
Keys are the header names.
3. JSON null becomes which Python value?
null ↔ None.
4. The finally block runs…
finally always runs – good for cleanup.
5. random.seed(1) is used to…
A fixed seed gives the same random numbers every run – essential for reproducible data science.

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.
➡️ Next

Day 7: put the whole week together in a mini project – a personal expense analyzer in pure Python.

Frequently Asked Questions

Should I use the csv module or pandas?
For quick scripts and huge files read row by row, csv is fine. For analysis, pandas (from Day 10) is far easier and faster.
Why newline="" when writing CSV?
It stops Windows from adding blank lines between rows.
What is the difference between json.load and json.loads?
load reads from a file object; loads (load string) parses a JSON text string.
← Data Science (60 Days)
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

Next topicDay 7: Mini Project: Expense Analyzer Your progress: 6 / 60 (10%) · Lesson 7 / 60 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free