file handling fh none tryfh open(filename" "encoding="utf "for incident in self values()narrative "\njoin(wrapper wrapincident narrative strip())fh write("[{ report_id}]\ "date={ date! }\ "aircraft_id={ aircraft_id}\ "aircraft_type={ aircraft_type}\ "airport={airport}\ "pilot_percent_hours_on_type="{ pilot_percent_hours_on_type}\ "pilot_total_hours={ pilot_total_hours}\ "midair={ midair: }\nnarrative_start \ {narrative}\nnarrative_end \ \nformat(incidentairport=incident airport strip()narrative=narrative)return true the line breaks in the narrative text are not significantso we can wrap the text as we like normally we would use the textwrap module' textwrap wrap(functionbut here we need to both indent and wrapso we begin by creating textwrap textwrap objectinitialized with the indentation we want to use (four spaces for the first and subsequent linesby defaultthe object will wrap lines to width of charactersalthough we can change this by passing another keyword argument datetime module str format( __format__( we could have written this using triple quoted stringbut we prefer to put in the newlines manually the textwrap textwrapper object provides wrap(method that takes string as inputin this case the narrative textand returns list of strings with suitable indentation and each no longer than the wrap width we then join this list of lines into single string using newline as the separator the incident date is held as datetime date objectwe have forced str format(to use the string representation when writing the date--this very conveniently produces the date in iso yyyy-mm-dd format we have told str format(to write the midair bool as an integer--this produces for true and for false in generalusing str format(makes writing text very easy because it handles all of python' data types (and custom types if we implement the __str__(or __format__(special methodautomatically