Recap: Station Logbook
Put together writing and reading files, with, reading line by line, and JSON
Mission: Station Logbook
Every event on the station goes into a log, one line each: a time and what happened, like 08:15 dock opened. At the end of the day the computer counts the events of each hour and keeps that summary as JSON, for the weekly report
- Start your program with a variable events, a list of strings, each a time
HH:MM, a space and a text. For exampleevents = ["08:15 dock opened", "08:40 crew lunch", "13:05 dock closed"] - Write the events to
events.txt, one on each line, with awithblock - Read
events.txtline by line and count the events of each hour: the hour is the two digits before the colon. Keep the hours in the order they first appear - Save the counts to
summary.jsonwithjson.dump(), then load them back withjson.load() - From the loaded data, print one line for each hour: the hour,
:and its number of events. Then printBusiest:and the hour with the most events, the first one if several have as many. Then printBytes:and how many characters the text ofsummary.jsonhas
events has from 1 to 20 events, already in time order
Example for the events above
08: 2
13: 1
Busiest: 08
Bytes: 18
The text of summary.json is {"08": 2, "13": 1}, which has 18 characters