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)