# main execution body def main(): # define output file and csv writer output_file = open('output_2Feb2018.csv', 'w', encoding='utf8') csv_writer = csv.writer(output_file) # save initial headers into the file csv_writer.writerow(['Date', 'Time', 'Location', 'Operator', 'Flight', 'Route', 'AC_Type', 'Registration', 'cn_ln', 'Aboard', 'Fatalities', 'Ground', 'Summary']) # save to csv in batches for yc in content_by_year: rows = [process_crash_html(crash_html) for crash_html in yc] # make sure that we filter out None records as these cause problems for csv writer csv_writer.writerows(list(filter(lambda x: x is not None, rows))) main()