file handling while truereport_id unpack_string(fhfalseif report_id is nonebreak data {data["report_id"report_id for name in ("airport""aircraft_id""aircraft_type""narrative")data[nameunpack_string(fhother_data fh read(numbersstruct sizenumbers numbersstruct unpack(other_datadata["date"datetime date fromordinal(numbers[ ]data["pilot_percent_hours_on_type"numbers[ data["pilot_total_hours"numbers[ data["midair"numbers[ incident incident(**dataself[incident report_idincident return true the while block loops until we run out of data we start by trying to get report id if we get none we've reached the end of the file and can break out of the loop otherwisewe create dictionary called data to hold the data for one incident and attempt to get the rest of the incident' data for the strings we use the unpack_string(methodand for the other data we read it all in one go using the numbersstruct struct since we stored the date as an ordinal we must do the reverse conversion to get date back but for the other itemswe can just use the unpacked data--no validation or conversion is required since we wrote the correct data types in the first place and have read back the same data types using the format held in the numbersstruct struct if any error occursfor exampleif we fail to unpack all the numbersan exception will be raised and will be handled in the except block (we haven' shown the except and finally blocks because they are structurally the same as those shown in the preceding subsection for the import_pickle(method mapping unpacking toward the end we make use of the convenient mapping unpacking syntax to create an incident object which we then store in the incidents dictionary apart from the handling of variable length stringsthe struct module makes it very easy to save and load data in binary format and for variable length strings the pack_string(and unpack_string(methods shown here should serve most purposes perfectly well