API to help make public data easier to access. Idea started with need for King County, WA COVID-19 data in API format while only having an excel file for source data.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

33 lines
1.1 KiB

import psycopg2
import csv
from datetime import datetime
import creds
# assign secrets to variables
dbhost = creds.dblogin['host']
database = creds.dblogin['db']
username = creds.dblogin['username']
password = creds.dblogin['password']
port = creds.dblogin['port']
conn = psycopg2.connect(f"host={dbhost} dbname={database} user={username} password={password} port={port}")
cur = conn.cursor()
# take the source data csv and upload to the database.
# using date variable to choose file
data_date = "20200831"
source_file = "./source_data/covid-19_" +data_date+"_zip.csv"
with open(source_file, 'r') as f:
reader = csv.reader(f)
next(reader) # Skip the header row.
for row in reader:
cur.execute(
"INSERT INTO king_county_covid19_zip(date_data, zipcode, population, tests, test_rate, \
positives, positive_rate, hospitalizations, hospitalization_rate, deaths, death_rate) \
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
(data_date,row[0],row[1],row[2],row[3],row[4],row[5],row[6],row[7],row[8],row[9])
)
conn.commit()