问题描述
我试图找出一种方法来跳过文件中的下两行,如果第一行中的条件为真。任何想法在一个很好的方式来做到这一点?这是我迄今为止...
$ p $ def main():
file = open(r'C: \Users\test\Desktop\test2.txt','r +')
ctr = 1
for current_line in file:
assert ctr if current_line [0:6] == str(001IU):
传递
else:
if ctr == 1 and current_line [9:11] == str(00):
做一些事情...
ctr + = 1
elif ctr == 1 and current_line [9:11]!= str(00):
pass #I want它跳过循环中的下两行
elif ctr == 2:
做某事...
ctr = 1
else:
raise ValueError
在Python 2.6或更高版本中,使用
下一个(文件)
下一个(文件)
跳过迭代器文件的两个项目 code>,即接下来的两行。
I am trying to figure out a way to skip the next two lines in a file if a condition in the first line is true. Any ideas on a good way to do this? Here's what I have so far...
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
In Python 2.6 or above, use
next(file)
next(file)
to skip two items of the iterator file
, i.e. the next two lines.
这篇关于用for循环读取文件时跳过一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!