Lessons and the learning game

Files, CSV and JSON

Build a weather balloon flight computer: safe file writes, a live sensor logged to CSV, settings in JSON, and code that survives disk errors.

The course

Files, CSV and JSON is an intermediate Coding course in seven lessons. Every lesson runs on the gallery's Raspberry Pi Zero 2 W with a BME280 on I2C, the flight computer of Skylark 3, a high-altitude balloon (a Zero flies because it weighs 11 grams). The catalog names a Pi 4; the gallery's only Pi with a BME280 is the Zero, so the course runs on it. Each step is checked against what the Pi prints.

  1. Reading files: with open, read, readlines and the hidden \n, a loop that skips blanks and comments, a key: value parser that keeps the colon in 09:30, and the bytes underneath with "rb".
  2. Writing safely: "w" that wipes a file, "a" that appends, print(..., file=f), and an atomic save with os.replace that keeps the last good copy through a fake power cut.
  3. CSV in and out: a recorded flight read with DictReader, pressure turned into altitude (up to 26 km, and the stratosphere getting warmer), the live BME280 logged with csv.writer, a header written only once, and commas and quotes inside values.
  4. JSON config: json.load, defaults filled with dict and update, validation that reports every problem at once, an atomic save with indent and sort_keys, and what JSON changes on the way (number keys come back as strings).
  5. Paths and folders: the working folder /home/pi, dated folders with makedirs, numbered log names that never overwrite, pathlib, and os.walk with sizes.
  6. Errors on disk: a missing config, a config cut off mid-save and moved aside as .bad, FileExistsError and IsADirectoryError, bad CSV rows counted with try, else and finally, and the OSError family by errno.
  7. Project: a sensor log tool: the finale.

The finale

The project is the tool Skylark's team runs before launch, typed to in the Serial Monitor. It loads config.json over its defaults, makes its folder, and answers help, log N (N live readings into a new numbered CSV, one every interval_s seconds), list (each log with its rows and size) and summary (every log read back, bad rows skipped and counted, the result saved as summary.json atomically). The last step cools the BME280 to 2 C and the logger raises the alarm set in config.json. Then comes an honest datasheet note: a BME280 reads pressure only down to 300 hPa, about 9 km.

What the Pi's engine does differently

The Pi runs MicroPython 1.29 with a CPython-style standard library:

  • Your tabs are written to /home/pi when Run starts. Files a program writes last until it ends; the next Run starts from your tabs again.
  • os, os.path, pathlib, glob, csv and json work as in CPython. Open mode "x" is missing, and a file's modification time does not change when you write to it.
  • A dict literal cannot unpack another dict ({**a, **b}), so defaults merge with dict(a) and update(b), which works everywhere.
  • The json reader accepts a comma after the last item, which CPython rejects, and its errors always say line 1, column 1.
  • A dict keeps no order of its own, so tables loop over sorted(...) and DictWriter fixes the column order with fieldnames.
  • str.zfill is missing; f"{n:03d}" pads numbers on both engines.