/ /ジオピーとエラー処理 - python、geopy

Geocopyとエラー処理 - 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を除いて"このような状況に対処する印象を受けましたが、私は間違っている必要があります。

助けを借りてくれてありがとう!

P.S.私はこれをコードから取り除いたが、実際に何を意味するのか分からない:

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

回答:

回答№1は5

今すぐ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):

(再び、add geocoders.google. エラー場所があなたのネームスペースにない場合は、すべてのジオピーエラーの前にある場所を指定します。

可能性のある例外をすべてキャッチしたい場合は、単に次のようにします。

except:

しかし、これは一般に悪い習慣です。なぜなら、 "あなたの構文エラー place, (lat, lng) = g.geocode(a) あなたが捕まえたくないので、それは良いですジオコードを調べて、キャッチしたい例外をすべてスローすることができるようにします。うまくいけば、それらのすべてがあなたが見つけたコードのビットにリストされています。