[Tutor] Text Processing Query
Mitya Sirenef
msirenef at lightbird.net
Thu Mar 14 16:33:20 CET 2013
On 03/14/2013 07:28 AM, taserian wrote:
> Since the identifier and the item that you want to keep are on different lines, you'll need to set
a "flag".
>
> with open(filename) as file:
>
> scanfile=file.readlines()
>
> flag = 0
>
> for line in scanfile:
>
> if line[0:6]=='COMPND' and 'FAB FRAGMENT' in line: flag = 1
>
> elif line[0:6]=='COMPND' and 'CHAIN' in line and flag = 1:
>
> print line
>
> flag = 0
>
>
> Notice that the flag is set to 1 only on "FAB FRAGMENT", and it's
reset to 0 after the next "CHAIN" line that follows the "FAB FRAGMENT" line.
I would simplify this a bit as follows:
flag = 0
for line in scanfile:
if line.strip():
if 'FAB FRAGMENT' in line:
flag = 1
elif flag:
print line
flag = 0
This assumes CHAIN line always follows MOLECULE line (otherwise elif
needs to check for CHAIN as well), it also ignores blank lines.
-m
--
Lark's Tongue Guide to Python: http://lightbird.net/larks/
It is pleasant at times to play the madman.
Seneca
More information about the Tutor
mailing list