python for循环文件每一行_python – 在使用for循环读取文件时跳过一行
生活随笔
收集整理的這篇文章主要介紹了
python for循环文件每一行_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 for循环文件每一行_python – 在使用for循环读取文件时跳过一行的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 洛谷 深基 第1部分 语言入门 第5章
- 下一篇: 信奥中的数学:二次互反律