python - 具有错误处理功能的 Geopy

标签 python geopy

我有一些带有错误处理的 Python 代码,但出于某种原因,代码似乎仍然无法处理这个特定错误:

raise GQueryError("No corresponding geographic location could be found for the     specified location, possibly because the address is relatively new, or because it may be incorrect.")
geopy.geocoders.google.GQueryError: No corresponding geographic location could be found for the specified location, possibly because the address is relatively new, or because it may be incorrect.

这是来源:

import csv
from geopy import geocoders
import time

g = geocoders.Google()

spamReader = csv.reader(open('locations.csv', 'rb'), delimiter='\t', quotechar='|')

f = open("output.txt",'w')

for row in spamReader:
    a = ', '.join(row)
    #exactly_one = False
    time.sleep(1)

    try:
        place, (lat, lng) = g.geocode(a)
    except ValueError:
        #print("Error: geocode failed on input %s with message %s"%(a, error_message))
        continue 

    b = str(place) + "," + str(lat) + "," + str(lng) + "\n"
    print b
    f.write(b)

我是否没有包含足够的错误处理?我的印象是“除了 ValueError”可以处理这种情况,但我肯定错了。

在此先感谢您的帮助!

附言我从代码中提取了这个,但我还不知道它的真正含义:

   def check_status_code(self,status_code):
    if status_code == 400:
        raise GeocoderResultError("Bad request (Server returned status 400)")
    elif status_code == 500:
        raise GeocoderResultError("Unkown error (Server returned status 500)")
    elif status_code == 601:
        raise GQueryError("An empty lookup was performed")
    elif status_code == 602:
        raise GQueryError("No corresponding geographic location could be found for the specified location, possibly because the address is relatively new, or because it may be incorrect.")
    elif status_code == 603:
        raise GQueryError("The geocode for the given location could be returned due to legal or contractual reasons")
    elif status_code == 610:
        raise GBadKeyError("The api_key is either invalid or does not match the domain for which it was given.")
    elif status_code == 620:
        raise GTooManyQueriesError("The given key has gone over the requests limit in the 24 hour period or has submitted too many requests in too short a period of time.")

最佳答案

现在 try/except 只捕获 ValueError。要同时捕获 GQueryError,请将 except ValueError: 行替换为:

except (ValueError, GQueryError):

或者如果 GQueryError 不在你的命名空间中,你可能需要这样的东西:

except (ValueError, geocoders.google.GQueryError):

或者捕获 ValueError 和 check_status_code 中列出的所有错误:

except (ValueError, GQueryError, GeocoderResultError, 
        GBadKeyError, GTooManyQueriesError):

(同样,添加 geocoders.google. 或任何错误位置到所有 geopy 错误的前面,如果它们不在您的命名空间中。)

或者,如果您只想捕获所有可能的异常,您可以简单地这样做:

except:

但这通常是不好的做法,因为它还会在您的 place, (lat, lng) = g.geocode(a) 行中捕获诸如语法错误之类的东西,而您却不知道不想被捕获,所以最好检查 geopy 代码以找到它可能抛出的所有你想捕获的异常。希望所有这些都列在您找到的那段代码中。

关于python - 具有错误处理功能的 Geopy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10776834/

相关文章:

python - 数半金字塔 Python

python - 列和行操作 Python Pandas

python - 按道路名称交叉点进行地理编码

openstreetmap - OSM在Python中获取从路线A到B的持续时间和方向

python - 删除列表中最大的数字并打印

python - 更改 PyCharm 中的默认窗口位置

python-3.x - 如何在python中对纬度和经度数据进行聚类(或删除不需要的数据)?

python - 用于 Python 的 geopy openmapquest 抛出 GeocoderInsufficientPrivileges 错误

python - 获取 DataFrame 的不同输出和相同代码的正常实现

python - 如何找到两个特殊字符之间的字符串?