使用Python检索街道地址的美国邮政邮政编码

时间:2010-09-07 23:45:51

标签: python gis

使用Python检索街道地址的美国邮政邮政编码最有效的方法是什么?这有可能吗?

最好是包含本地数据库的东西,与某种远程API调用相反。

提前感谢您提供任何帮助。

2 个答案:

答案 0 :(得分:4)

可能是一个开始: The Zip Code Database Project

googlemaps – Google Maps and Local Search APIs in Python

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

给定字符串地址查询,返回信息字典   关于那个位置,包括它   纬度和经度。     有趣的位:

>>> 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

答案 1 :(得分:0)

更可靠的方法是使用使用当前USPS地址数据的强大地址验证服务。在完全披露中,我为SmartyStreets工作,这是一家提供just such a service的地址验证软件公司。这是一个简单的例子,说明了如何使用该服务:

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

从样本中可以看出,此服务提供完整的9位邮政编码以及所有其他地址组件以及有关该地址的一些元数据。