def export_dataframe_to_csv(name: str, dataframe): filename = os.path.join(CSV_FOLDER, f'{name}.csv') old_hash = sha1sum(filename) dataframe.replace({ 0: None }).astype('Int64').to_csv(filename, line_terminator='\r\n') write_timestamp_file(filename=filename, old_hash=old_hash)
old_hash = sha1sum(municipality_deceased_csv_path) with open(file=municipality_deceased_csv_path, encoding='utf-8') as f: rows = [row for row in csv.DictReader(f)] latest_date = str([val for val in df.index.values][-1]).split('T')[0] latest_date = datetime.strptime(latest_date, '%Y-%m-%d').date() while (date := datetime.strptime(rows[-1]['date'], '%Y-%m-%d').date()) < latest_date: rows.append(copy.deepcopy(rows[-1])) rows[-1]['date'] = str(date + timedelta(days=1)) # Write the rows collection back to the csv with open(municipality_deceased_csv_path, 'w', newline='', encoding='utf-8') as csvfile: writer = csv.DictWriter(csvfile, fieldnames=rows[0].keys()) writer.writeheader() for row in rows: writer.writerow(row) write_timestamp_file(filename=municipality_deceased_csv_path, old_hash=old_hash) # --- region-confirmed.csv | region-active.csv --- df = pd.read_excel(io=SOURCE_FILE, sheet_name='Tabela 4', engine='openpyxl', skiprows=[0, 2])[:-1] df.drop(['SKUPAJ'], inplace=True, axis=1) # axis=1 means columns def get_region_header(region: str): return { 'Pomurska': 'region.ms.todate', 'Podravska': 'region.mb.todate', 'Koroška': 'region.sg.todate', 'Savinjska': 'region.ce.todate', 'Zasavska': 'region.za.todate', 'Posavska': 'region.kk.todate', 'Jugovzhodna Slovenija': 'region.nm.todate',
for key in [ 'kranj', 'ljubljana', 'domzale', 'saleske', 'koper', 'celje', 'maribor' ]: if key in components: return key raise Exception('No location key found: ', components) mapping = {} for column in df.columns: if column == 'NIB Measurements': mapping['NIB Measurements'] = 'measurements.nib' continue components = column.lower().split('_') location = get_location_key(components=components) components.remove(location) location = { 'saleske': 'velenje' }.get(location, location) # transform some of the location names mapping[column] = f'sewage.{location}.{"-".join(components)}' df.rename(mapper=mapping, axis='columns', inplace=True) # export to csv filename = os.path.join(CSV_FOLDER, 'sewage.csv') old_hash = sha1sum(filename) df.to_csv(filename) write_timestamp_file(filename=filename, old_hash=old_hash)
old_hash = sha1sum(deceased_regions_csv_path) with open(deceased_regions_csv_path) as f: rows = [row for row in csv.DictReader(f)] latest_date = str([val for val in df.index.values][-1]).split('T')[0] latest_date = datetime.strptime(latest_date, '%Y-%m-%d').date() while (date := datetime.strptime(rows[-1]['date'], '%Y-%m-%d').date()) < latest_date: rows.append(copy.deepcopy(rows[-1])) rows[-1]['date'] = str(date + timedelta(days=1)) # Write the rows collection back to the csv with open(deceased_regions_csv_path, 'w', newline='') as csvfile: writer = csv.DictWriter(csvfile, fieldnames=rows[0].keys()) writer.writeheader() for row in rows: writer.writerow(row) write_timestamp_file(filename=deceased_regions_csv_path, old_hash=old_hash) # --- regions-cases.csv | regions-cases-active.csv --- df = pd.read_excel(io=SOURCE_FILE, sheet_name='Tabela 4', engine='openpyxl', skiprows=[0, 2])[:-1] df.drop(['SKUPAJ'], inplace=True, axis=1) # axis=1 means columns def get_region_header(region: str): return { 'Pomurska': 'region.ms.todate', 'Podravska': 'region.mb.todate', 'Koroška': 'region.sg.todate', 'Savinjska': 'region.ce.todate', 'Zasavska': 'region.za.todate', 'Posavska': 'region.kk.todate', 'Jugovzhodna Slovenija': 'region.nm.todate',