![](/img/trans.png)
[英]How to skip error and continue to run for loop and continue to the next line
[英]How to skip AttributeError in for loop python and continue the loop
我使用'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數據。
我找到了答案,這在我的案例中有效。
回答:
if IMEI is None:
continue
如果返回值為None,這有助於跳過循環,在我的情況下,AttributeError意味着相同。
try:
execute_something()
except AttributeError:
caught_error()
這就是我們在python中捕獲錯誤的方法。 只需在for循環中添加上面的代碼段即可。 用我們自己的代碼替換我使用的功能。
如果出現問題,請多詢問一下。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.