Skip to content

Instantly share code, notes, and snippets.

@steven-terrana
Created November 14, 2022 19:22
Show Gist options
  • Save steven-terrana/a231fc4408a25b41c24cded91c13e442 to your computer and use it in GitHub Desktop.
Save steven-terrana/a231fc4408a25b41c24cded91c13e442 to your computer and use it in GitHub Desktop.
lookup zip codes from addresses
# import module
from geopy.geocoders import Nominatim
import csv
import re
# initialize Nominatim API
geolocator = Nominatim(user_agent="geoapiExercises")
filename = 'data.csv'
with open(filename, 'r') as csvfile:
datareader = csv.reader(csvfile)
for row in datareader:
street = row[4]
city = row[5]
state = row[6]
if "N/A" in city or "N/A" in state:
continue
address = f'{street} {city} {state}'
location = geolocator.geocode(address)
if location:
data = location.raw
loc_data = data['display_name']
print(loc_data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment