if we are not in narrative and are not reading new report id there are only three more possibilitieswe are reading key=value itemswe are at narrative start markeror something has gone wrong in the case of reading line of key=value datawe split the line on the first characterspecifying maximum of one split--this means that the value can safely include characters all the data read is in the form of unicode stringsso for datenumericand boolean data types we must convert the value string accordingly for dates we use the datetime datetime strptime(function ("string parse time"which takes format string and returns datetime datetime object we have used format string that matches the iso date formatand we use datetime datetime date(to retrieve datetime date object from the resultant datetime datetime objectsince we want only date and not date/time we rely on python' built-in type functionsfloat(and int()for the numeric conversions notethough thatfor exampleint(" "will raise valueerrorif we want to be more liberal in accepting integerswe could use int(float(" "))or if we wanted to round rather than truncateround(float(" ")to get bool is slightly subtler--for examplebool(" "returns true ( nonempty string is true)so we must first convert the string to an int invalidmissingor out-of-range values will always cause an exception to be raised if any of the conversions fail they raise valueerror exception and if any values are out of range an incidenterror exception will be raised when the data is used to create corresponding incident object if the line doesn' contain an characterwe check to see whether we've read the narrative start marker if we havewe set the narrative variable to be an empty string this means that the first if condition will be true for subsequent linesat least until the narrative end marker is read if none of the if or elif conditions is true then an error has occurredso in the final else clause we raise keyerror exception to signify this return true except (environmenterrorvalueerrorkeyerrorincidenterroras errprint("{ }import error{ }formatos path basename(sys argv[ ])err)return false finallyif fh is not nonefh close(after reading all the lineswe return true to the caller--unless an exception occurredin which case the except block catches the exceptionprints an error