def export_latest_data(d_ref_states, d_states_data: dict): d_states_latest = helper.extract_latest_data(d_ref_states, d_states_data) # # d_states_latest = dict(d_ref_states) # for code in d_states_latest.keys(): # assert code in d_states_data.keys() # l_state = d_states_data[code] # d_latest = l_state[-1] # d_states_latest[code]['Date_Latest'] = d_latest['Date'] # for key in ('Cases', 'Deaths', 'Cases_New', 'Deaths_New', 'Cases_Per_Million', 'Deaths_Per_Million'): # d_states_latest[code][key] = d_latest[key] with open('data/de-states/de-states-latest.tsv', mode='w', encoding='utf-8', newline='\n') as fh: csvwriter = csv.DictWriter(fh, delimiter='\t', extrasaction='ignore', fieldnames=('State', 'Code', 'Population', 'Pop Density', 'Date_Latest', 'Cases', 'Deaths', 'Cases_New', 'Deaths_New', 'Cases_Per_Million', 'Deaths_Per_Million') ) csvwriter.writeheader() for code in sorted(d_states_latest.keys()): d = d_states_latest[code] d['Code'] = code if code == 'DE-total': # DE as last row d_de = dict(d) continue csvwriter.writerow( d ) del d, code # add # to uncomment the DE total sum last line d_de['State'] = '# Deutschland' csvwriter.writerow(d_de) del d_de
def export_latest_data(d_districts_data: dict): d_districts_latest = helper.extract_latest_data(d_ref_landkreise, d_districts_data) d_for_export_V1 = d_districts_latest l_for_export_V2 = [] for lk_id, d in d_districts_latest.items(): # V1: dict (lk_id) -> dict # V2: list of ficts # d_for_export_V1[lk_id] = d d["Landkreis"] = get_lk_name_from_lk_id(lk_id) d["Bundesland"] = d["BL_Name"] del d["BL_Name"] # divi data is not returned by helper.extract_latest_data and mostly not available at latest day, so using the date of the previous day instead if 'DIVI_Intensivstationen_Covid_Prozent' in d_districts_data[lk_id][ -1]: d['DIVI_Intensivstationen_Covid_Prozent'] = d_districts_data[ lk_id][-1]['DIVI_Intensivstationen_Covid_Prozent'] d['DIVI_Intensivstationen_Betten_belegt_Prozent'] = d_districts_data[ lk_id][-1]['DIVI_Intensivstationen_Betten_belegt_Prozent'] elif 'DIVI_Intensivstationen_Covid_Prozent' in d_districts_data[lk_id][ -2]: d['DIVI_Intensivstationen_Covid_Prozent'] = d_districts_data[ lk_id][-2]['DIVI_Intensivstationen_Covid_Prozent'] d['DIVI_Intensivstationen_Betten_belegt_Prozent'] = d_districts_data[ lk_id][-2]['DIVI_Intensivstationen_Betten_belegt_Prozent'] d_for_export_V2 = d d_for_export_V2['LK_ID'] = lk_id l_for_export_V2.append(d_for_export_V2) # Export as JSON helper.write_json('data/de-districts/de-districts-results.json', d_for_export_V1, sort_keys=True) helper.write_json( filename='data/de-districts/de-districts-results-V2.json', d=l_for_export_V2, sort_keys=True) # Export as CSV with open('data/de-districts/de-districts-results.tsv', mode='w', encoding='utf-8', newline='\n') as fh_csv: csvwriter = csv.DictWriter( fh_csv, delimiter='\t', extrasaction='ignore', fieldnames=[ 'Landkreis', 'Bundesland', 'Population', 'Cases', 'Deaths', 'Cases_Per_Million', 'Deaths_Per_Million', 'DIVI_Intensivstationen_Covid_Prozent', 'DIVI_Intensivstationen_Betten_belegt_Prozent' ]) csvwriter.writeheader() for lk_id, d in d_for_export_V1.items(): csvwriter.writerow(d)
def extract_latest_date_data(): """ for all countries in json: extract latest entry write to data/int/countries-latest-all.tsv and data/int/countries-latest-all.json """ d_countries_latest = helper.extract_latest_data(d_countries_ref, d_countries_timeseries) l_for_export = [] with open('data/int/countries-latest-all.tsv', mode='w', encoding='utf-8', newline='\n') as fh: csvwriter = csv.DictWriter( fh, delimiter='\t', extrasaction='ignore', fieldnames=[ 'Country', 'Population', 'Date', 'Cases', 'Deaths', 'Cases_Per_Million', 'Deaths_Per_Million', 'Cases_Last_Week_Per_Million', 'Deaths_Last_Week_Per_Million', 'Continent', 'Code', 'DoublingTime_Cases_Last_Week_Per_100000' ]) # 'Cases_Last_Week', csvwriter.writeheader() for country in sorted(d_countries_latest.keys(), key=str.casefold): # l_time_series = d_countries_timeseries[country] # d = l_time_series[-1] # last entry (=>latest date) # pop = read_population(country) d2 = d_countries_latest[country] d2['Country'] = country # d2['Code'] = read_country_code(d2['Country']) # d2['Continent'] = read_continent(d2['Country']) # d2['Population'] = pop # if d2['Cases_Per_Million']: # d2['Cases_Per_Million'] = round( # d['Cases_Per_Million'], 0) # if d2['Deaths_Per_Million']: # d2['Deaths_Per_Million'] = round( # d['Deaths_Per_Million'], 0) # if d2['Cases_Last_Week_Per_Million']: # d2['Cases_Last_Week_Per_Million'] = round( # d['Cases_Last_Week_Per_Million'], 0) # if d2['Deaths_Last_Week_Per_Million']: # d2['Deaths_Last_Week_Per_Million'] = round( # d['Deaths_Last_Week_Per_Million'], 0) csvwriter.writerow(d2) l_for_export.append(d2) # JSON export helper.write_json(filename='data/int/countries-latest-all.json', d=l_for_export) # for selected countries write to separate file, for Gnuplot plotting with open('data/int/countries-latest-selected.tsv', mode='w', encoding='utf-8', newline='\n') as fh: csvwriter = csv.DictWriter(fh, delimiter='\t', extrasaction='ignore', fieldnames=[ 'Country', 'Date', 'Population', 'Cases', 'Deaths', 'Cases_Per_Million', 'Deaths_Per_Million' ]) csvwriter.writeheader() for country in sorted(d_selected_countries.keys(), key=str.casefold): l_time_series = d_countries_timeseries[country] d = l_time_series[-1] # last entry for this country d2 = d d2["Country"] = country d2['Population'] = d_selected_countries[country]['Population'] csvwriter.writerow(d2)