你能“重启"吗?Python循环的当前迭代?

2022-01-24 00:00:00 python iteration for-loop

问题描述

有没有办法实现这样的东西:

Is there a way to implement something like this:

for row in rows:
    try:
        something
    except:
        restart iteration


解决方案

你可以把你的 try/except 块放在另一个循环中,然后在它成功时中断:

You could put your try/except block in another loop and then break when it succeeds:

for row in rows:
    while True:
        try:
            something
            break
        except Exception: # Try to catch something more specific
            pass

相关文章