Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 读取带有for循环的文件时跳过一行_Python_File Io_For Loop_Iterator - Fatal编程技术网

Python 读取带有for循环的文件时跳过一行

Python 读取带有for循环的文件时跳过一行,python,file-io,for-loop,iterator,Python,File Io,For Loop,Iterator,我试图找出一种方法,如果第一行中的条件为真,则跳过文件中的下两行。有什么好办法吗?这是我到目前为止所拥有的 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"): pa

我试图找出一种方法,如果第一行中的条件为真,则跳过文件中的下两行。有什么好办法吗?这是我到目前为止所拥有的

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
def main():
file=open(r'C:\Users\test\Desktop\test2.txt,'r+')
ctr=1
对于文件中的当前_行:
断言中心

我会这样做…

在Python2.6或更高版本中,使用

next(file)
next(file)

跳过迭代器
文件
的两项,即下两行。

如果我在读取特定文件时只想看到从第7行到前一行(文件的总行数-4)之间的数据
next(file)
next(file)