Intermediate level · Module 1 of 6 · 16 guided hours
Files and Structured Data in Python
Work with the file formats organisations actually use: plain text, CSV and JSON.
Learning objectives
- Read and write text files safely with context managers
- Process CSV files with the csv module
- Store and load structured data as JSON
Topics
- open() and with blocks
- file paths with pathlib
- csv.reader and csv.DictReader/DictWriter
- json.load and json.dump
- text encodings (UTF-8)
Lessons
-
Lesson 1 Reading and Writing Text Files
Open, read, append and write text files using with blocks and pathlib paths.
context managerfile mode r/w/apathlib.PathUTF-8 encoding
-
Lesson 2 Processing CSV Files
Read spreadsheet exports as CSV, filter and summarise rows, and write new CSV files.
CSVheader rowDictReader/DictWriternewline=''
-
Lesson 3 Saving and Loading JSON
Use JSON to save program data between runs and to exchange data with web services.
serialisationjson.dump/json.loadnested dataindentation for readability
Practical activity
Convert a KoboToolbox-style survey export (CSV) into a JSON summary by district, then reload the JSON and print a formatted report.
Expected outcomes
- Reliable reading and writing of common file formats
- Data that persists between program runs
Assessment
Practical exercise checked against a provided expected output file.
Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.