Skip to content

Instantly share code, notes, and snippets.

@Radcliffe
Last active August 29, 2015 14:07
Show Gist options
  • Save Radcliffe/fe70d1d10ecd4cc55db5 to your computer and use it in GitHub Desktop.
Save Radcliffe/fe70d1d10ecd4cc55db5 to your computer and use it in GitHub Desktop.
Standardized addresses using SmartyStreets
# Address Standardization with SmartyStreets
import urllib2
import urllib
import json
# Note: Replace these lines with valid credentials
AUTH_ID = "########-####-####-####-############"
AUTH_TOKEN = "####################"
# Function standardized_address:
#
# Input: One or more of the following keyword arguments:
# street, city, state, zipcode
# The values should be strings.
#
# Output: A dictionary which contains the following keys, among others:
# delivery_point_barcode:
# 12-digit POSTNET barcode. Consists of 5-digit ZIP code,
# 4-digit addon code, 2-digit delivery point, and 1-digit check digit.
# delivery_line_1:
# First line of address
# delivery_line_2:
# Second line of address (if required)
# last_line:
# City, state, and ZIP combined
# For more information, read the API documentation at http://smartystreets.com/kb/liveaddress-api
def standardize_address(**address):
BASE_URL = "https://api.smartystreets.com/street-address"
data = {'auth-id': AUTH_ID,
'auth-token': AUTH_TOKEN,
'candidates': '1'
}
data.update(address)
url_values = urllib.urlencode(data)
full_url = BASE_URL + '?' + url_values
response = urllib2.urlopen(full_url)
the_page = response.read()
result = json.loads(the_page)
if isinstance(result, list) and len(result) > 0:
return result[0]
if __name__ == "__main__":
# Test
std = standardize_address(
street = '806 Carla Lane',
city = 'Little Canada',
state = 'MN',
zipcode = '55109')
for key in ('delivery_point_barcode', 'delivery_line_1',
'delivery_line_2', 'last_line'):
if key in std:
print std[key]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment