Lab: Wrangle some data
Read a CSV of product inventory, filter low-stock items, and write the results to JSON — the complete parse-transform-serialise loop.
- Parse a CSV string with csv.DictReader into a list of dicts
- Filter rows based on a numeric field
- Transform records by adding a computed field
- Serialise filtered results to a formatted JSON string
You have a CSV export of product inventory. Your job: find items that are running low on stock, add a computed urgency field, and write the results to JSON so a downstream system can process them.
This lab exercises the complete data-wrangling loop: parse → filter → transform → serialise.
The data
sku,name,category,stock,reorder_threshold
A001,Wireless Keyboard,peripherals,14,20
A002,USB-C Hub,peripherals,5,15
A003,Monitor Stand,accessories,32,10
A004,Webcam HD,peripherals,2,10
A005,Desk Lamp,accessories,0,5
A006,Laptop Sleeve,accessories,18,25
A007,Mechanical Keyboard,peripherals,7,20
A008,Mouse Pad XL,accessories,11,10A product needs reordering when stock < reorder_threshold. The urgency is
reorder_threshold - stock — how many units short you are.
Checkpoint 1 — parse the CSV
Read every row into a list of dicts and print the first two:
Checkpoint 2 — filter and convert types
Find products that need reordering. Remember: every value from CSV is a string,
so convert stock and reorder_threshold to integers before comparing:
Checkpoint 3 — transform and add a computed field
Build a clean output record for each low-stock item. Add the computed urgency
field and drop the raw CSV strings in favour of typed values:
Checkpoint 4 — serialise to JSON
Write the final output as formatted JSON:
The structure mirrors the automation lab: separate functions for parsing,
processing, and output. If the CSV later comes from a real file, only
parse_inventory() changes — the filtering and serialisation logic stays intact.
Extend it yourself
- Group the reorder list by
categoryin the JSON output instead of a flat list. - Add a
"generated_at"timestamp field to the output usingdatetime.datetime.now().isoformat(). - Change
parse_inventory()to accept a file path and read from a real CSV on disk.
Parsing JSON and CSV
Python's built-in json and csv modules turn raw data strings into dictionaries you can filter, transform, and write back out.
HTTP fundamentals
HTTP is the language of the web — understanding its request/response cycle, methods, status codes, and headers is the foundation of every API integration.