python 单行读取文件_python – 在使用for循环读取文件时跳过一行
我試圖找出一種方法,如果第一行中的條件為真,則跳過文件中的下兩行.有什么想法可以做到這一點嗎?這是我到目前為止所擁有的……
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
解決方法:
在Python 2.6或更高版本中,使用
next(file)
next(file)
跳過迭代器文件的兩個項目,即接下來的兩行.
標簽:python,iterator,file-io,for-loop
來源: https://codeday.me/bug/20190626/1293537.html
超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生總結
以上是生活随笔為你收集整理的python 单行读取文件_python – 在使用for循环读取文件时跳过一行的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: asp sql查询过滤空格_对比Exce
- 下一篇: python调用函数示例_python