Commit adb7a8ec authored by Thanassis Drivas's avatar Thanassis Drivas

Upload Main Script

parent 682f4034
import requests
import csv
from datetime import datetime
# set output filename
today = str(datetime.now())
outputFilename = 'results' + today + '.csv'
# ask and get data
response = requests.get("https://opendata.ecdc.europa.eu/covid19/casedistribution/csv")
data = response.content
# transform data from string to array
data = data.split('\n')
headings = data[0]
values = data[1:]
# create dictionaries based on ID
countriesCases = {}
countriesDeaths = {}
# loop over data
for datarow in values:
datarow = datarow.split(',')
id = datarow[7]
if id in countriesCases:
countriesCases[id] += int(datarow[4])
countriesDeaths[id] += int(datarow[5])
else:
countriesCases[id] = int(datarow[4])
countriesDeaths[id] = int(datarow[5])
# create lists of lists sorted by country ID
toWrite = [["ID", "CASES", "DEATHS"]]
for key in sorted(countriesCases.keys()):
toWrite.append([key, countriesCases[key], countriesDeaths[key]])
# write results to file (alternatively: to database)
with open(outputFilename, "wb") as f:
writer = csv.writer(f)
writer.writerows(toWrite)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment