question 177 Exceptions & Validation · medium
Weather station readings with an error log
Weather stations upload one reading per line: date,station,temperature. Group the temperatures by station, and keep a log of the lines that couldn't be read, with Python's own message, so the station operator can see what was wrong.
Function
readings_by_station(lines)linestexts like
"2025-01-10,KSEA,7.5"; spaces around fields should be ignoredReturns
(by_station, log): {station: [temperatures as floats]} in first-seen order, and ["line <n>: <error message>"] numbered from 1Bad linesa line that doesn't split into exactly three fields, or whose temperature isn't a number: both raise ValueError somewhere
Examples
callreadings_by_station(['2025-01-10,KSEA,7.5', '2025-01-10,KORD,-3.0', '2025-01-10,KSEA', '2025-01-11,KSEA, 8.25', '2025-01-11,KORD,n/a'])returns({'KSEA': [7.5, 8.25], 'KORD': [-3.0]}, ['line 3: not enough values to unpack (expected 3, got 2)', "line 5: could not convert string to float: 'n/a'"])
callreadings_by_station(['2025-02-01,KJFK,0'])returns({'KJFK': [0.0]}, [])
callreadings_by_station([])returns({}, [])