Python geopy geocoders.Google

守給你的承諾、 提交于 2020-01-24 00:31:32

问题


I am trying to run a Python script using Geopy that creates a list of coordinates. I have installed Geopy, and am running from Terminal on a Mac.

python
from geopy import geocoders
import csv
g_api_key = 'I HAVE ENTERED MY GOOGLE API HERE’
g = geocoders.Google(g_api_key)

I then get the error message:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'Google'

Could my API key be wrong? Why is this happening? If I didn't receive this message, I would load the .csv next:

costcos = csv.reader(open('costcos-limited.csv'), delimiter=',')
next(costcos) #skip header
#print header
print "Address,City,State,Zip Code,Latitude,Longitude"
continue
full_addy = row[1] + "," + row[2] + "," + row[3] + "," + row[4]
try:
place, (lat, lng) = list(g.geocode(full_addy, exactly_one=False))[0]
print full_addy + "," + str(lat) + "," + str(lng)
except:
print full_addy + ",NULL,NULL"

Is this code correct, and is the 'continue' (above 'full_addy') necessary in this code? Finally, if I get help to make the 'geocoders.Google' work, and this script works, how do you run a Python script? I.e. I've been writing these commands into Terminal, how do I run the script on the final 'print full_addy + ",NULL,NULL"' line and save the output as costcos-geocoded.csv?

Thank you in advance for any help that comes my way...


回答1:


The 'module' object has no attribute 'Google' error occurred because you are using new version of geopy which does not have Google class but GoogleV3, which allows to use API version 3.

Just use:

g = geocoders.GoogleV3(g_api_key)

To write a python script instead of writing code into python shell just save your code into script.py file and run it from terminal:

python script.py

or if you want to save the output of that script to a file:

python script.py > output_file.txt



来源:https://stackoverflow.com/questions/26281733/python-geopy-geocoders-google

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!