Learning map · Track 8 · intermediate
Files, CSV and JSON
Most data a program works with does not come from the keyboard. Price lists, sales exports, log files, HR spreadsheets and settings all arrive as files, and most results worth keeping leave a program as files too. This track teaches you to read and write text files safely, find your way around folders with pathlib, and stream through files far too large to load at once. You then move on to the two formats you will meet everywhere: CSV, the lowest common denominator of spreadsheets and database exports, and JSON, the language of web APIs and configuration files.
Real files are messy, so a large part of the track is about trust: checking every record for missing fields, wrong types and impossible values, collecting clear error reports instead of crashing, and coping with missing, empty or badly encoded files. It is written for learners who are comfortable with functions, lists, dictionaries and string methods. Every exercise runs your code against real files in a private working folder, including deliberately broken ones, and the track ends with a mini project that consolidates sales exports from several shop branches into a summary CSV and a JSON report.
By the end of this track you can
- Read and write text files with the right encoding, using with blocks, write and append modes and pathlib paths
- Process large files line by line to count, filter and transform records without loading everything into memory
- Read and write CSV files with csv.reader, csv.DictReader, csv.writer and csv.DictWriter, converting text fields to proper types
- Load, navigate, build and save JSON data, including nested objects and lists
- Validate records for required fields, types and ranges, and handle missing, empty and malformed files gracefully
- Build small, reusable read-clean-transform-write pipelines that combine several input files into reports
Lessons
- 1Reading text filesOpen a text file, read it all at once or line by line, handle newline characters and choose the right encoding.30 min · 4 exFree
- 2The with statement and writing filesLet with close files for you, create and overwrite files with mode w, add to them with mode a, and control newlines.35 min · 4 exMembers
- 3Paths and folders with pathlibBuild file paths with pathlib.Path, check what exists, list files with glob, create folders and read or write text in one call.35 min · 4 exMembers
- 4Processing files line by lineStream through large files one line at a time to count, filter, transform and write results without loading the whole file.40 min · 4 exMembers
- 5Reading CSV filesRead spreadsheet-style CSV data with csv.reader and csv.DictReader, handle headers, quoting and delimiters, and convert text fields to numbers.40 min · 4 exMembers
- 6Writing CSV filesProduce correct CSV files with csv.writer and csv.DictWriter: headers, automatic quoting, newline="" and missing or extra fields.35 min · 4 exMembers
- 7Working with JSONRead and write JSON with json.load, json.loads, json.dump and json.dumps, map JSON types to Python, and navigate nested data.45 min · 5 exMembers
- 8Validating recordsCheck every record for required fields, correct types, sensible ranges and duplicates, and collect clear errors per row instead of crashing.45 min · 4 exMembers
- 9Missing, empty and broken filesHandle files that are missing, empty, malformed or in an unexpected encoding, so one bad input does not stop the whole program.40 min · 4 exMembers
- 10Small data pipelinesStructure file-processing programs as a pipeline of small functions: read, clean, transform and write, each testable on its own.45 min · 4 exMembers
- 11Mini project: consolidating branch salesCombine messy CSV exports from several shop branches into a validated summary CSV and a JSON report, one milestone at a time.75 min · 5 exMembers