【发布时间】:2018-12-11 07:06:01
【问题描述】:
我有一个文件~/practice/search_from,看起来像这样:
From i
ssdfadfksjaflkf
asdfasf
adf
sd
fd
fs
sgdggggggggggggsd
gsg
sdg
From j
dasdfewf
sdfas
adsf
我想打印以 From 开头的行。
所以我在 python 提示符下做了以下操作:
>>> fhandle=open('practice/search_from')
>>> for line in fhandle:
... if not line.startswith('From '):
... continue
... else:
... print(line.rstrip())
...
From i
From j
这段代码似乎工作正常。
但是,当我把
>>> fhandle=open('practice/search_from')
>>> for line in fhandle:
... line = line.rstrip()
... if not line.startswith('From:') :
... continue
... print(line)
...
或
>>> fhandle=open('practice/search_from')
>>> for line in fhandle:
... line = line.rstrip()
... if not line.startswith('From:') :
... continue
... else:
... print(line)
没有打印出来。为什么会这样?有没有办法修复最后两个代码?
非常感谢。
【问题讨论】:
标签: python python-3.x continue