【问题标题】:Geopy with error handling带有错误处理的 Geopy
【发布时间】:2012-05-27 20:02:36
【问题描述】:

我有一些带有错误处理的 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.")

【问题讨论】:

    标签: python geopy


    【解决方案1】:

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

    except (ValueError, GQueryError):
    

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

    except (ValueError, geocoders.google.GQueryError):
    

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

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

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

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

    except:
    

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

    【讨论】:

    • 多么棒的答案。我喜欢它。谢谢。
    • 从 geopy 导入地理编码器,除了(ValueError、geocoders.google.GQueryError、geocoders.google.GeocoderResultError、geocoders.google.GBadKeyError、geocoders.google.GTooManyQueriesError):
    • 不客气!是的,你是对的,我错过了你在做from geopy import geocoders 而不是import geopy。已编辑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-19
    • 2013-04-20
    • 1970-01-01
    • 2016-12-07
    • 2014-02-27
    • 2018-03-31
    • 1970-01-01
    相关资源
    最近更新 更多