Skip to content Skip to sidebar Skip to footer

How To Continue After An Exception? Python

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

Solution 1:

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)

Post a Comment for "How To Continue After An Exception? Python"