runtime
453 / 500
question 453 Reliable Data Pipelines · easy

Parse arrival delays without losing a row

An airline sends its daily delays as text lines: a header, then flight_id,arrival_delay per flight. Cancelled flights have an empty delay. Load every good row and report the line numbers of the rows you couldn't read, instead of crashing or skipping them silently.

Functionparse_delays(lines)
linesline 1 is the header flight_id,arrival_delay; the rest are data rows
Good rowexactly two fields; the flight ID is a whole number; the delay is a whole number (it may be negative) or empty, which becomes None
Returns(delays, rejected): a dictionary from flight ID (int) to delay, and the line numbers (counting the header as line 1) of rows that aren't good
Rulesblank lines are skipped without a report; spaces around a field are ignored
Examples
callparse_delays(['flight_id,arrival_delay', '12207,-4', '12208,12', '12209,', '12210,n/a', '12211,-3,x'])returns({12207: -4, 12208: 12, 12209: None}, [5, 6])
callparse_delays(['flight_id,arrival_delay', '1,0'])returns({1: 0}, [])
callparse_delays(['flight_id,arrival_delay', 'x,5', '2,7'])returns({2: 7}, [2])

Loading your progress…

Parse arrival delays without losing a row — Python practice · Runtime