尝试从数据框中读取数据时,Python挂起

时间:2017-09-18 17:00:36

标签: python

我正在遍历文件位置的DataFrame。如果我找到了我想要的文件,那么我会尝试解压缩它,然后尝试重命名它。如果失败,那么我想继续到数据帧的下一行。问题是如果我打电话给"继续"然后,它跳回到前一个for循环,然后我的程序挂起,而不是跳回到主循环。有没有办法指定哪个for循环可以在出现错误时回退?

这是我的伪代码:

for index, row in df.iterrows(): #if something fails I want to go back here
    #get the file location       
    for file in location: #"continue" is taking me back to here which I don't want
        #search for file
        if file found:
            file_found = True
    if not file_found:
        continue
    else:
        file_found = False
    try:
        #unzip file
    except:
        #could not unzip
        continue
    try:
        #rename unzipped file
    except:
        try:
            #try renaming a different way
        except:
            #could not rename file
            continue     

1 个答案:

答案 0 :(得分:1)

最简单的解决方法是使用break,它会退出其封闭循环。