异常后如何继续?蟒蛇

时间:2023-01-11 08:40:54

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.

我有一个for循环,解析从Yelp中提取的数据。有时,由于某些商业名称,我得到以下异常:'ascii'编解码器无法编码位置3中的字符u'\ xf1':序数不在范围内(128)。由于这很常见,我希望我的for循环在跳过抛出异常的行之后继续迭代。什么是最好的方式?出于某种原因使用继续对我不起作用。

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)

1 个解决方案

#1


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

您可以尝试在while循环内移动try / except

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)

#1


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

您可以尝试在while循环内移动try / except

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)