I have a file ~/practice/search_from
that looks like this:
From i
ssdfadfksjaflkf
asdfasf
adf
sd
fd
fs
sgdggggggggggggsd
gsg
sdg
From j
dasdfewf
sdfas
adsf
I want to print lines that starts with From.
So I did in the python prompt the following :
>>> fhandle=open('practice/search_from')
>>> for line in fhandle:
... if not line.startswith('From '):
... continue
... else:
... print(line.rstrip())
...
From i
From j
This code seems to work fine.
However, when I put
>>> fhandle=open('practice/search_from')
>>> for line in fhandle:
... line = line.rstrip()
... if not line.startswith('From:') :
... continue
... print(line)
...
or
>>> fhandle=open('practice/search_from')
>>> for line in fhandle:
... line = line.rstrip()
... if not line.startswith('From:') :
... continue
... else:
... print(line)
nothing prints out. Why is it like this? Is there a way to fix the last two codes?
Thank you very much.
Your codes are fine, except, you are searching with From:
.
Remove the colon(:)
from your codes, and it will work properly:
In [2296]: fhandle=open('practice/search_from')
In [2297]: for line in fhandle:
...: line = line.rstrip()
...: if not line.startswith('From'):
...: continue
...: print(line)
...:
From i
From j