Python geopy地理编码器.Google [英] Python geopy geocoders.Google

查看:199
本文介绍了Python geopy地理编码器.Google的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用创建坐标列表的Geopy运行Python脚本. 我已经安装了Geopy,并且正在Mac上的Terminal中运行.

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)

然后我收到错误消息:

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

我的API密钥可能不正确吗?为什么会这样呢?如果未收到此消息,则接下来将加载.csv:

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"

此代码正确吗,并且此代码中的继续"(在"full_addy"之上)是否必要? 最后,如果我得到帮助以使"geocoders.Google"正常运行并且此脚本有效,那么您如何运行Python脚本? IE.我已经将这些命令写入Terminal,如何在最后的'print full_addy +,NULL,NULL"'行上运行脚本并将输出保存为costcos-geocoded.csv?

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

推荐答案

发生'module' object has no attribute 'Google'错误的原因是您使用的是geopy的新版本,该版本没有Google类,但具有GoogleV3,该版本允许使用API版本3.

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.

只需使用:

g = geocoders.GoogleV3(g_api_key)

要编写python脚本而不是将代码编写到python shell中,只需将代码保存到script.py文件中并从终端运行它即可:

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

这篇关于Python geopy地理编码器.Google的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆