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

时间:2014-07-16 08:39:48

标签: python for-loop error-handling try-except

我使用' for循环'记录数组中多个文件的IMEI号。

当但是当没有检测到IMEI时,for循环停止指示AttributeError:rint。

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

我使用过这种方法:

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

我想要做的是在文件中未检测到IMEI时跳过错误,然后继续循环查找其他文件中的IMEI。

IMEI是指16Digit AlphaNumeric代码。我将它用作'字符串'。

有3200' .dat'我处理过的文件,用于在每个文件中查找这样的Alpha数字文本。 每个dat文件都包含一些HEX数据。

2 个答案:

答案 0 :(得分:2)

我找到了答案,这在我的案例中有效。

答案:

if IMEI is None:
    continue

如果返回值为None,这有助于跳过循环,在我的情况下,AttributeError意味着相同。

答案 1 :(得分:1)

try:
    execute_something()
except AttributeError:
    caught_error()

这是我们在python中捕获错误的方法。 只需在for循环中添加上面的代码段即可。 用我们自己的代码替换我使用的函数。

如果出现问题,请多询问。