简体   繁体   English

如何在for循环python中跳过AttributeError并继续循环

[英]How to skip AttributeError in for loop python and continue the loop

I using a 'for loop' to record the IMEI number from multiple files in an array. 我使用'for循环'来记录数组中多个文件的IMEI号。

When, but when no IMEI is detected the for loop stops indicating AttributeError:rint. 当但是当没有检测到IMEI时,for循环停止指示AttributeError:rint。

    result = getattr(asarray(obj), method)(*args, **kwds)
    AttributeError: rint

I have used this method: 我用过这个方法:

for IMEI in file():
    try:
        detect the IMEI from the files()
        Append them to the array()
    exception AttributeError:
        print 'File name'
        pass

What I would like to do is to skip the error if the IMEI is not detected in the file and then continue with the loop looking for IMEI in other files. 我想要做的是如果在文件中没有检测到IMEI,则跳过错误,然后继续循环查找其他文件中的IMEI。

IMEI refers to 16Digit AlphaNumeric code. IMEI引用16Digit AlphaNumeric代码。 I use it as a 'string'. 我用它作为'字符串'。

There are 3200 '.dat' files which I processed for finding such a Alpha Numeric text in each file. 我处理了3200个'.dat'文件,用于在每个文件中查找这样的Alpha数字文本。 Each dat file has some HEX data in it. 每个dat文件中都包含一些HEX数据。

I found the answer, which works in my case. 我找到了答案,这在我的案例中有效。

Answer: 回答:

if IMEI is None:
    continue

This helps in skipping the loop if the return value is None and as in my case the AttributeError meant the same. 如果返回值为None,这有助于跳过循环,在我的情况下,AttributeError意味着相同。

try:
    execute_something()
except AttributeError:
    caught_error()

This is how we catch an error in python. 这就是我们在python中捕获错误的方法。 Just add the above code snippet in your for loop. 只需在for循环中添加上面的代码段即可。 Replace the functions i used with your own code. 用我们自己的代码替换我使用的功能。

Please ask more if something is still wrong. 如果出现问题,请多询问一下。

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

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