Getting a US Postal System ZIP Code for a Street Address Using Python

What is the most efficient way to get the US zip code for a mailing address using Python? Is it possible, what is possible?

Preferably, something that includes a local database somehow resists remote API calls.

Thanks in advance for any help you can offer.

+4
source share
2 answers

May Begin: Zip Database Project

googlemaps - Google Maps and local search APIs in Python

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

Given the string address query, return a dictionary of information about this 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 
+4
source

A more reliable way is to use a reliable address verification service that uses current USPS address data. In full disclosure, I work for SmartyStreets , an address verification company that provides just such a service . Here is a simple example illustrating how to use the service:

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

As you can see from the example, this service provides a full 9-digit postal code along with all other address components, as well as some metadata about the address.

0
source

All Articles