Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import csv
- from collections import defaultdict, Counter
- from time import time
- import re
- from datetime import datetime
- por_fecha = defaultdict(Counter)
- RE_FECHA = re.compile(r'[0-9]+([A-Z]+[0-9]+)\:.*')
- with open('casos_covid19.csv', newline='', encoding='utf-8-sig') as csvfile:
- reader = csv.DictReader(csvfile)
- i = 0
- start = time()
- for row in reader:
- fecha_raw = row['fecha_toma_muestra'] or row['fecha_clasificacion']
- fecha = RE_FECHA.match(fecha_raw).group(1)
- por_fecha[fecha][row['clasificacion']] += 1
- i+=1
- if i % 10000 == 0:
- print('.', end='')
- end = time()
- print()
- print(f'Tomó {end-start:.3} segundos para {i} registros')
- print(f"{'fecha':^7} {'%+':^6} {'total':^6} -- detalle")
- for fecha, cont in sorted(por_fecha.items(), key=lambda kv: datetime.strptime(kv[0], '%b%Y')):
- total = sum(cont.values())
- cont_str = ''.join(f'{k:>11}:{v:<6}' for k, v in sorted(cont.items(), key=lambda kv: kv[0]))
- print(f"{fecha:7} {cont['confirmado']/total:6.1%} {total:6} -- {cont_str}")
Add Comment
Please, Sign In to add comment