Python, properly · Chapter 18 of 42

JSON and CSV

By the end of this chapter you can read and write JSON and CSV files with Python's json and csv modules, and turn DabbaDrop's messy exports into clean data you can add up and trust. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Code that holds up
  • 5 tasks
  • Python
  • In the all labs plan

The first 2 chapters of Python, properly are free. This one comes with the all labs plan.

Why you need this

Rafiq asked a simple question: how much did DabbaDrop earn from delivered orders in September? The answer is in orders.csv, but the export is messy. Some totals are written Rs 477 or ₹230 instead of 477, some statuses are DELIVERED instead of delivered, and order D0101 appears twice, so adding up the column naively gives a wrong number or a crash. Bhavna's customer list has phone numbers in four formats and a customer who signed up twice.

Chapter 17 read files as plain text. JSON and CSV files have structure (records, fields, quoting rules), and splitting lines by hand breaks on the first awkward value. Python's json and csv modules know the rules; cleaning the values is then your job.

What you build: 5 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    menu_report.py: what is on the menu

    Farah wants a two-line summary of the menu. Read /workspace/data/menu.json with the json module and print exactly:

    <n> dishes, <a> available

    Unavailable: <names>

    where n is the number of dishes, a how many have available set to true, and names the names of the others, in the menu's order, separated by a comma and a space.

  2. Task 2

    clean_total.py: one messy total

    orders.csv writes most totals as 240, but some as Rs 240 or ₹240. Write parse_total(text) that returns the total in rupees as an int: parse_total("240"), parse_total("Rs 240"), parse_total("₹240") and parse_total(" Rs 90 ") return 240, 240, 240 and 90.

    Text with no digits at all, like "free" or "", raises a ValueError whose message contains the text.

    The check runs it on every total in orders.csv.

  3. Task 3

    clean_orders.py: every order, once and clean

    Rafiq needs the orders clean before he adds anything up. Write clean_orders(path): it reads the orders CSV at path and returns a list with one dict per order, in the file's order, with the file's column names as keys, where:

    total is an int (Rs 240 and ₹240 become 240); status is lowercase with no spaces around it (DELIVERED becomes delivered); every other value stays the string it was in the file.

    An order whose order_id was already seen is skipped: the first copy wins (D0101 appears twice in orders.csv).

    The check runs it on /workspace/data/orders.csv and on a small file of its own.

  4. Task 4

    clean_customers.py: a clean customer file

    Bhavna wants a customer list she can trust. Write clean_customers(src, dest): read the customers CSV at src and write a CSV at dest with the header customer_id,name,phone,email,area and one line per customer, where:

    phone is the last 10 digits of the number (07089496252, 83262-72722, +91 84641 29694 and 8709470176 become 7089496252, 8326272722, 8464129694 and 8709470176); email is lowercase with no spaces around it; area stays as it is, blank included; joined is left out.

    A customer whose phone (once cleaned) belongs to an earlier customer signed up twice: skip them, keeping the first. Return how many customers were written.

    The check runs it on /workspace/data/customers.csv.

  5. Task 5 · interview question

    Interview: hour_counts.py, number keys through JSON

    The kitchen wants to know how many orders arrive in each hour, saved as JSON for the dashboard. Write two functions: save_counts(counts, path) writes counts, a dict from an hour (an int like 13) to a number of orders, to path as JSON; load_counts(path) reads that file and returns the same dict, with int keys and int values.

    The check saves the real hour counts from orders.csv, loads them back, and compares the result to what it saved.

    Careful: JSON only allows strings as object keys. Be ready to explain what json.dump does to the key 13, and what that means when you read the file back.

What you will learn

  • JSON: json.loads and json.load to read, json.dumps and json.dump to write, and how JSON types become Python types.
  • CSV: csv.reader and csv.DictReader to read, csv.DictWriter to write, and why every value comes back as a string.
  • Cleaning: one small function per messy field, and removing duplicate records.

More in Code that holds up

Build "JSON and CSV" yourself

DabbaDrop, a Mumbai tiffin startup, runs on Excel and WhatsApp. You are its first Python developer. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Python, properly are free. This one comes with the all labs plan.