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

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to