Created
September 14, 2020 22:13
-
-
Save liamstrilchuk/576dc577df9610fab38497ddf7213e3d to your computer and use it in GitHub Desktop.
Get CSV file (needs owid-covid-data.csv)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
def main(): | |
days = {} | |
countries = ["United States", "India", "Brazil", "Russia", "Peru"] | |
with open("owid-covid-data.csv") as f: | |
reader = csv.reader(f) | |
for line in reader: | |
try: | |
float(line[5]) | |
if line[2] == "International" or line[2] == "World": | |
continue | |
if line[3] not in days: | |
days[line[3]] = {} | |
if line[2] not in days[line[3]]: | |
days[line[3]][line[2]] = int(float(line[5])) | |
except: | |
continue | |
day_results = {} | |
for day in days: | |
day_results[day] = {} | |
for country in countries: | |
if not country in days[day]: | |
day_results[day][country] = 0 | |
continue | |
day_results[day][country] = days[day][country] | |
total_non_cases = 0 | |
for country in days[day]: | |
if not country in countries: | |
total_non_cases += days[day][country] | |
day_results[day]["Other"] = total_non_cases | |
f = open("covid_results.csv", "w") | |
f.write("day,United States,India,Brazil,Russia,Peru,Other\n") | |
f.close() | |
days_sorted = sorted(day_results) | |
f = open("covid_results.csv", "a") | |
for day in days_sorted: | |
f.write(f"{day},{day_results[day]['United States']},{day_results[day]['India']},{day_results[day]['Brazil']},{day_results[day]['Russia']},{day_results[day]['Peru']},{day_results[day]['Other']}\n") | |
f.close() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment