1

I have a for loop that's parsing data being pulled from Yelp. Sometimes, I get the following exception due to some of the business names:'ascii' codec can't encode character u'\xf1' in position 3: ordinal not in range(128). Since this is pretty common, I would like my for loop to continue iterating after this exception skipping the line that's throwing the exception. What would be the best way? For some reason using continue doesn't work for me.

try:
    response = json.loads(conn.read())
    yelp_data =[x for x in response['businesses']]
    logger.info('Connection to yelp was made')
    data_len = len(yelp_data)
    while data_len > 0:
        for line in yelp_data:
            address = [x.encode('UTF8') for x in line['location']['display_address']]
            cat =[','.join([str(c) for c in lst]) for lst in line['categories']]
            Category =','.join(cat)
            target.submit( 'Name = {},Rating = {},URL = {},Review_Count = {},Phone = {},Yelp_ID = {},Address = {}, Category = {}\n'.format(line.get('name'),line.get('rating'),line.get('mobile_url'),line.get('review_count'),line.get('phone'),line.get('id'),", ".join(address), Category),sourcetype=sourcetype)
            data_len = data_len -1

except Exception as e:
    logger.error(e)
Mitch
  • 177
  • 1
  • 6
  • On which line do you get the exception? If it happens inside your loop, you can put your try / except inside the loop. – Adalee May 08 '15 at 17:22
  • Relevant: [Catching specific exceptions](http://stackoverflow.com/q/13531247/198633), [Handle exception, print message, carry on](http://stackoverflow.com/q/2005680/198633) – inspectorG4dget May 08 '15 at 17:24
  • it's the target.submit line. The name is sometimes in Spanish so I'll get an encoding exception. – Mitch May 08 '15 at 19:31

1 Answers1

3

You can try to move the try/except inside the while loop

while data_len > 0:
    try:
        for line in yelp_data:
            address = [x.encode('UTF8') for x in line['location']['display_address']]
            cat =[','.join([str(c) for c in lst]) for lst in line['categories']]
            Category =','.join(cat)
            target.submit( 'Name = {},Rating = {},URL = {},Review_Count = {},Phone = {},Yelp_ID = {},Address = {}, Category = {}\n'.format(line.get('name'),line.get('rating'),line.get('mobile_url'),line.get('review_count'),line.get('phone'),line.get('id'),", ".join(address), Category),sourcetype=sourcetype)
            data_len = data_len -1

    except Exception as e:
        logger.error(e)
Jthorpe
  • 9,756
  • 2
  • 49
  • 64
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331