简体   繁体   English

在try…中捕获异常后如何继续循环

[英]How to continue a loop after catching exception in try … except

I am reading a big file in chunks and I am doing some operations on each of the chunks. 我正在分块读取一个大文件,并且正在对每个块进行一些操作。 While reading one of get them I had the following message error: 读取其中之一时,出现以下消息错误:

pandas.errors.ParserError: Error tokenizing data. pandas.errors.ParserError:标记数据时出错。 C error: Expected 26 fields in line 15929977, saw 118 C错误:在15929977行中预期有26个字段,看到了118

which means that one of my file lines doesn't follow the same format as the others. 这意味着我的文件行之一与其他文件行没有采用相同的格式。 What I thought I could do was to just omit this chunk but I couldn't get a way to do it. 我以为我可以做的就是忽略这一部分,但是我没有办法做到。 I tried to do a try/except block as follows: 我试图做一个try/except块,如下所示:

data = pd.read_table('ny_data_file.txt', sep=',', 
                      header=0, encoding = 'latin1', chunksize = 5000)
try: 
    for chunk in data:
           # operations
except pandas.errors.ParseError:
           # Here is my problem

What I have written here is my problem is that if the chunk is not well parsed, my code will automatically go to the exception not even entering the for loop , but what I would like is to skip this chunk and move forward to the next one, on which I would like to perform the operations inside the loop. 我在这里写的是我的问题是,如果该块没有得到很好的解析, 我的代码将自动进入异常,甚至不进入for循环 ,但我想跳过此块并前进到下一个,我想在其中执行循环内的操作。

I have checked on stackoverflow but I couldn't find anything similar where the try was performed on the for loop. 我已经检查了stackoverflow,但是在for循环上进行尝试的地方找不到类似的东西。 Any help would be appreciated. 任何帮助,将不胜感激。

UPDATE: 更新:

I have tried to do as suggested in the comments: 我已尝试按照评论中的建议进行操作:

try:
    for chunk in data:
        #operations
except pandas.errors.ParserError:
        # continue/pass/handle error

But still is not cathching the exception because as said the exception is created when getting the chyunk out of my data not when doing operations with it. 但是仍然不能捕获异常,因为如上所述,异常是在从我的数据中获取chyunk时创建的,而不是在对其进行操作时创建的。

I understood that, in the operations part you get exception. 我了解到,在操作部分您会获得例外。 If it is like that: you should just continue: 如果是这样:您应该继续:

for chunk in data:
    try:
       # operations
    except pandas.errors.ParseError:
       # continue

The way you use try - except makes it skip the entire for loop if an exception is caught in it. try - except使用try - except的方式try - except如果发现异常,将跳过整个for循环。 If you want to only skip one iteration you need to write the try-except inside the loop like so: 如果只想跳过一个迭代,则需要在循环内编写try-except,如下所示:

for chunk in data:
    try:
       # operations
    except pandas.errors.ParseError as e:
        # inform the user of the error
        print("Error encountered while parsing chunk {}".format(chunk))
        print(e)

I am not sure where the exception is thrown. 我不确定在哪里抛出异常。 Maybe adding a full error stack would help. 也许添加一个完整的错误堆栈会有所帮助。 If the error is thrown by the read_table() call maybe you could try this: 如果该错误是由read_table()调用引发的,则可以尝试以下操作:

try: 
    data = pd.read_table('ny_data_file.txt', sep=',', 
                      header=0, encoding = 'latin1', chunksize = 5000)

except pandas.errors.ParseError:
           pass
for chunk in data:
           # operations

正如@JonClements什么解决我的问题建议是使用error_bad_lines=Falsepd.read_csv所以它只是跳过该行造成的麻烦,让我执行的,其余为循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM