Twitch
Twitch

Reputation: 693

Prevent error from halting a Python script

Using Python 2.6, the following error occurs when I run the far below script:

Traceback (most recent call last):
  File "g.py", line 7, in <module>
    results  = Geocoder.geocode(row[0])
  File "/usr/lib/python2.6/site-packages/pygeocoder.py", line 261, in geocode
    return GeocoderResult(Geocoder.getdata(params=params))
  File "/usr/lib/python2.6/site-packages/pygeocoder.py", line 223, in getdata
    raise GeocoderError(j['status'], url)
pygeocoder.GeocoderError: Error ZERO_RESULTS
Query: http://maps.google.com/maps/api/geocode/json?region=&sensor=false&bounds=&language=&address=%22++A+FAKE+ADDRESS

The Python 2.6 script:

import csv, string
from pygeocoder import Geocoder

with open('file.csv') as goingGeo:
        theSpreadsheet = csv.reader(goingGeo, quotechar=None)
        for row in theSpreadsheet:
                results  = Geocoder.geocode(row[0])
                (lat, long) = results[0].coordinates
                with open('geo_file.csv', 'a') as f:
                        f.write(row[0] + ",")
                        f.write(row[1] + ",")
                        f.write(row[2] + ",")
                        f.write(row[3] + ",")
                        f.write(row[4] + ",")
                        f.write(row[5] + ",")
                        f.write(row[6] + ",")
                        f.write(row[7] + ",")
                        try:
                                f.write(str(lat))
                        except GeocoderError:            
                                pass
                        f.write(",")
                        try:
                                f.write(str(long))
                        except GeocoderError:            
                                pass
                        f.write('\n')

I just want the script to continue even with the error.

Thank you!

Upvotes: 0

Views: 2550

Answers (4)

user2325788
user2325788

Reputation: 1

#starting from line 6:
for row in theSpreadsheet:
        try:
            results  = Geocoder.geocode(row[0])
        except:
            pass
#rest of script . . .

you can also use "except" to deal with specific errors ex.

try:
    results=Geocoder.geocode(row[0])
except GeocodeError:
    #deal with error

Upvotes: 0

bonsaiviking
bonsaiviking

Reputation: 5995

You're on the right track with the try: except GeocoderError parts, but they're in the wrong places. You need to move them to wrap the Geocoder.geocode call, since that's what is throwing the error:

        for row in theSpreadsheet:
                try:
                        results  = Geocoder.geocode(row[0])
                except GeocoderError:
                        continue
                (lat, long) = results[0].coordinates

Also note that you'll need to import the GeocoderError name from pygeocoder. Also, long is a keyword in Python, so I would suggest picking a different name for that variable.

Upvotes: 0

user2286078
user2286078

Reputation:

Use the try-except-finally statement like this:

try:
    f.write(str(lat))
except GeocodeError:
    pass
finally:
    do_something_else_regardless_of_above

Upvotes: 0

Lee Daniel Crocker
Lee Daniel Crocker

Reputation: 13171

You have try/except blocks around a write call that can't possibly throw a GeoCoderError, but you don't have a try/except around the call to geocoder() that can (and apparently does) throw that error. You probably want something like:

try:
    results  = Geocoder.geocode(row[0])
    (lat, long) = results[0].coordinates
except GeocoderError:
    (lat, long) = (0.0, 0.0)

Upvotes: 1

Related Questions