Retrieving the US Postal Zip code for a street address using Python

Question:

What is the most efficient way one would go about retrieving the U.S. Postal zip code for a street address using Python? Is this something that is even possible?

Preferably, something that includes a local database as oppose to a remote API call of some sort.

Thanks in advance for any help one may be able to offer.

Asked By: Belmin Fernandez

||

Answers:

May be a start:
The Zip Code Database Project

googlemaps – Google Maps and Local Search APIs in Python

GoogleMaps.geocode(query, sensor='false', oe='utf8', ll='', spn='', gl='')

Given a string address query, return a dictionary of information
about that location, including its
latitude and longitude.
Interesting bits:

>>> gmaps = GoogleMaps(api_key)
>>> address = '350 Fifth Avenue New York, NY'
>>> result = gmaps.geocode(address)
>>> placemark = result['Placemark'][0]
>>> lng, lat = placemark['Point']['coordinates'][0:2]
# Note these are backwards from usual
>>> print lat, lng
40.6721118 -73.9838823
>>> details = placemark['AddressDetails']['Country']['AdministrativeArea']
>>> street = details['Locality']['Thoroughfare']['ThoroughfareName']
>>> city = details['Locality']['LocalityName']
>>> state = details['AdministrativeAreaName']
>>> zipcode = details['Locality']['PostalCode']['PostalCodeNumber']
>>> print ', '.join((street, city, state, zipcode))
350 5th Ave, Brooklyn, NY, 11215
Answered By: Leniel Maccaferri

A more reliable way is to use a robust address verification service that is using current USPS address data. In full disclosure, I work for SmartyStreets, an address verification software company which provides just such a service. Here’s a simple example that illustrates how to use the service:

https://github.com/smartystreets/LiveAddressSamples/blob/master/python/street-address.py

As you can see from the sample, this service provides the full 9-digit ZIP Code along with all other address components as well as some metadata about the address.

Answered By: Michael Whatcott

I know it’s been a long time since anyone has responded in this thread but you can try using the chatGPT API to retrieve a zip code from a street address. I have tried it and it seems to be more accurate than stuff like Nominatim and it’s free.

Answered By: snowball
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.