-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathLoad-ZipcodeGeo.py
47 lines (40 loc) · 1.26 KB
/
Load-ZipcodeGeo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import pyodbc, csv, sys
# Loads the geocode latitude/longitude to the NPPES database by
# zip code. These will be used for a best effort approach
# to geocode those NPI addresses that received a "No_Match"
# status from the Census geocoding system. It's far from accurate,
# but provides a facsimile that is close enough for my purpose.
inputfile = sys.argv[1]
userid = sys.argv[2]
password = sys.argv[3]
server = sys.argv[4]
writecount, readcount, fldnum = 0, 0, 0
connstr = f'Driver=ODBC Driver 17 for SQL Server;Server={server};UID={userid};PWD={password};Database=NPPES;'
sqlinsert1 = """
insert into dbo.zipgeo(
Zip,
City,
State,
Latitude,
Longitude,
Timezone
)
values (?, ?, ?, ?, ?, ?)
"""
print("Input File :", inputfile)
conn = pyodbc.connect(connstr)
cursor = conn.cursor()
with open(inputfile, encoding="utf8", errors='replace', mode='r') as csvinfile:
datareader = csv.reader(csvinfile, delimiter=';')
for row in datareader:
readcount += 1
if readcount == 1:
continue # skip header record
dbrec = [row[0], row[1], row[2], float(row[3]), float(row[4]), int(row[5])]
cursor.execute(sqlinsert1, dbrec)
if readcount % 1000 == 0:
print("Records Processed:", readcount)
conn.commit()
conn.commit()
conn.close()
print("Records read: ", readcount)