2012-05-27 53 views
1

我有錯誤處理一些Python代碼的地方,但由於某些原因的代碼似乎仍然無法處理此特定錯誤:Geopy與錯誤處理

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

回答

5

在try /除了只捕ValueError S:我把這個出代碼,但我不知道它真正的意思呢。爲了趕上GQueryError以及與更換except ValueError:行:

except (ValueError, GQueryError): 

或者,如果GQueryError是不是在你的命名空間,你可能需要像這樣:

except (ValueError, geocoders.google.GQueryError): 

或者趕上ValueError異常和所有的在check_status_code列出錯誤:

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

(同樣,添加geocoders.google.或任何錯誤位置是的前LL的geopy錯誤,如果他們不是在你的命名空間)

或者,如果你只是想捕獲所有可能的異常,你可以簡單地做:

except: 

但這是普遍不好的做法,因爲它還會捕獲您不想捕獲的place, (lat, lng) = g.geocode(a)行中的語法錯誤,因此最好檢查一下geopy代碼以查找它可能拋出的所有可能的異常,以便捕獲它們。希望所有這些都列在你找到的那部分代碼中。

+0

真是個好主意。我喜歡它。謝謝。 – Giuseppe

+0

from geopy import geocoders except(ValueError,geocoders.google.GQueryError,geocoders.google.GeocoderResultError,geocoders.google.GBadKeyError,geocoders.google.GTooManyQueriesError): – Giuseppe

+0

不客氣!是的,你是對的,我錯過了你從'geopy import geocoders'而不是'import geopy'。編輯。 – weronika