使用for循环读取文件时跳过一行

Lan*_*ins 5 python file-io iterator for-loop

我试图找出一种方法,如果第一行中的条件为true,则跳过文件中的后两行。有什么想法可以做到这一点吗?到目前为止,这就是我所拥有的...

def main():
    file = open(r'C:\Users\test\Desktop\test2.txt', 'r+')
    ctr = 1
    for current_line in file:
        assert ctr<3
        if current_line[0:6] == str("001IU"):
            pass
        else:
            if ctr == 1 and current_line[9:11] == str("00"):
                do something...
                ctr += 1
            elif ctr == 1 and current_line[9:11] != str("00"):
                pass #I want it to skip the next two lines in the loop
            elif ctr == 2:
                do something...
                ctr = 1
            else:
                raise ValueError
Run Code Online (Sandbox Code Playgroud)

Sve*_*ach 5

在 Python 2.6 或更高版本中,使用

next(file)
next(file)
Run Code Online (Sandbox Code Playgroud)

跳过迭代器的两项file,即接下来的两行。