2014-08-28 112 views
-2

地址我有變量地址字符串像這樣:提取房子地址城市和zip從Python中

1234 Maple Ave. Queens, NY 11011 

此格式字符串從變化到字符串,所以我可以用簡單的字符串解析/分割。 有沒有什麼辦法可以連接到谷歌地圖或其他api,它會查找地址並分別給我所有的地址單元?

理想我應該能夠做這樣的事情:

>> address = "1234 Maple Ave. Queens, NY 11011" 
>> data = maps_api.extract_data(address) 
>> print data.house_number 
>> 1234 Maple Ave. 
>> print data.city 
>> Queens 
>> print data.state 
>> NY 
>> print data.zip 
>> 11011 

回答

2

這對於googlemaps api似乎完美。

from googlemaps import GoogleMaps 
gmaps = GoogleMaps(api_key) 
address = "1234 Maple Ave. Queens, NY 11011" 

result = gmaps.geocode(address) 
placemark = result['Placemark'][0] 
details = placemark['AddressDetails']['Country']['AdministrativeArea'] 

city = details['Locality']['LocalityName'] 
state = details['AdministrativeAreaName'] 
zip = details['Locality']['PostalCode']['PostalCodeNumber'] 

儘管你需要一個API密鑰。

替代似乎有pygeocoder

相關問題