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.
- Reading files:
with open,read,readlinesand the hidden\n, a loop that skips blanks and comments, akey: valueparser that keeps the colon in09:30, and the bytes underneath with"rb". - Writing safely:
"w"that wipes a file,"a"that appends,print(..., file=f), and an atomic save withos.replacethat keeps the last good copy through a fake power cut. - 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 withcsv.writer, a header written only once, and commas and quotes inside values. - JSON config:
json.load, defaults filled withdictandupdate, validation that reports every problem at once, an atomic save withindentandsort_keys, and what JSON changes on the way (number keys come back as strings). - Paths and folders: the working folder
/home/pi, dated folders withmakedirs, numbered log names that never overwrite,pathlib, andos.walkwith sizes. - Errors on disk: a missing config, a config cut off mid-save and moved
aside as
.bad,FileExistsErrorandIsADirectoryError, bad CSV rows counted withtry,elseandfinally, and theOSErrorfamily byerrno. - 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/piwhen Run starts. Files a program writes last until it ends; the next Run starts from your tabs again. os,os.path,pathlib,glob,csvandjsonwork 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 withdict(a)andupdate(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(...)andDictWriterfixes the column order withfieldnames. str.zfillis missing;f"{n:03d}"pads numbers on both engines.