如何改进此代码以接受包含很多地址的文件,并将其作为参数传递给Google API以获取经/纬度?
现在,这是一个返回一个地址的经/纬度的示例,但我想自动处理数据集中的多个地址。
import requests
GOOGLE_MAPS_API_URL = 'http://maps.googleapis.com/maps/api/geocode/json'
params = {
'address': '100 FOUNDERS CV ,ALPHARETTA, GA,30022',
'sensor': 'false',
'region': 'USA'
}
# Do the request and get the response data
req = requests.get(GOOGLE_MAPS_API_URL, params=params)
res = req.json()
# Use the first result
result = res['results'][0]
geodata = dict()
geodata['lat'] = result['geometry']['location']['lat']
geodata['lng'] = result['geometry']['location']['lng']
geodata['address'] = result['formatted_address']
print('{address}. (lat, lng) = ({lat}, {lng})'.format(**geodata))
答案 0 :(得分:0)
您可以使用Google Maps Service的Python库来加快速度。有一个地理编码功能:https://github.com/googlemaps/google-maps-services-python/blob/master/googlemaps/geocoding.py
您可以将文件作为dict读入文件,并将每一行作为迭代的关键。