Python 如何在for循环的条件下强制执行异常处理?
我试图循环对象检查,这是一个从OpenLCA软件获得的特定数据库。该数据库的某些实例中存在某种缺陷。每当我运行for语句时,在循环许多实例之后,当代码遇到错误实例时,它会在for循环条件中抛出一个错误- 对于我的支票: 如何对for循环条件语句强制执行异常处理,如图所示。请帮忙 非常具体地说,循环中的语句不会引发错误。它是在check语句本身中为i抛出的。因此,在循环中尝试并不能帮助我解决这个问题 文件U:/Desktop/Box/Box Sync/Research\U compile/electricitylci/pythonapiopenlca.py,第101行,在 对于我的支票: 文件C:\ProgramData\Anaconda3\lib\site packages\olca\ipc.py,第105行,在get\u all中 e、 来自_jsonr 文件C:\ProgramData\Anaconda3\lib\site packages\olca\schema.py,第1882行,from\u json self.default_allocation_method=AllocationTypeval 文件C:\ProgramData\Anaconda3\lib\enum.py,第291行,在调用中 返回cls.newcls,值 文件C:\ProgramData\Anaconda3\lib\enum.py,第533行,新格式 返回cls.missingvalue 缺少文件C:\ProgramData\Anaconda3\lib\enum.py,第546行 raise VALUER错误%r不是有效的%s%值,cls.name ValueError:“NO_ALLOCATION”不是有效的AllocationType在for循环中使用try和except,如下所示:Python 如何在for循环的条件下强制执行异常处理?,python,Python,我试图循环对象检查,这是一个从OpenLCA软件获得的特定数据库。该数据库的某些实例中存在某种缺陷。每当我运行for语句时,在循环许多实例之后,当代码遇到错误实例时,它会在for循环条件中抛出一个错误- 对于我的支票: 如何对for循环条件语句强制执行异常处理,如图所示。请帮忙 非常具体地说,循环中的语句不会引发错误。它是在check语句本身中为i抛出的。因此,在循环中尝试并不能帮助我解决这个问题 文件U:/Desktop/Box/Box Sync/Research\U compile/elec
for i in check:
try:
process = i.default_allocation_method
name = i.name
print(name)
except Exception as e:
print("error is: {}".format(e))
check = client.get_all(olca.Process)
i = 0
while i < len(check): # or other function that returns length of check
try:
process = check[i].default_allocation_method
name = check[i].name
print(name)
except Exception as e:
print("error is: {}".format(e))
finally:
i += 1
def wrap_next(getter):
while True:
try:
yield getter.next()
except ValueError:
howl(misery, desperation)
for i in wrap_next(check)
... Do things
更新:如果在for loop语句中引发错误,并且即使在引发错误后仍要继续执行for loop,则如果check具有len且不是迭代器,则可以将其替换为while loop,并处理如下异常:
for i in check:
try:
process = i.default_allocation_method
name = i.name
print(name)
except Exception as e:
print("error is: {}".format(e))
check = client.get_all(olca.Process)
i = 0
while i < len(check): # or other function that returns length of check
try:
process = check[i].default_allocation_method
name = check[i].name
print(name)
except Exception as e:
print("error is: {}".format(e))
finally:
i += 1
def wrap_next(getter):
while True:
try:
yield getter.next()
except ValueError:
howl(misery, desperation)
for i in wrap_next(check)
... Do things
如果check是一个迭代器,那么您可以使用生成器函数see@tripleee answer来隔离只覆盖for语句的尝试。也许可以这样包装:
for i in check:
try:
process = i.default_allocation_method
name = i.name
print(name)
except Exception as e:
print("error is: {}".format(e))
check = client.get_all(olca.Process)
i = 0
while i < len(check): # or other function that returns length of check
try:
process = check[i].default_allocation_method
name = check[i].name
print(name)
except Exception as e:
print("error is: {}".format(e))
finally:
i += 1
def wrap_next(getter):
while True:
try:
yield getter.next()
except ValueError:
howl(misery, desperation)
for i in wrap_next(check)
... Do things
完美,特别是如果个人只想在之后继续:错误不会出现在循环中的语句中。我的错误是在检查声明中提出的:在这种情况下,按照你的建议做对我没有帮助。@TapajyotiGhosh那么它是从哪里来的呢?您的错误到底是什么?@shotgunner该错误在语句中抛出-因为我在检查:该语句调用了我正在使用的模块中的某些代码,这会引发ValueError。@TapajyotiGhosh。那你为什么不把try和except放在for循环之外呢?