En Mon, 19 Nov 2007 21:15:16 -0300, Henry <[EMAIL PROTECTED]>  
escribió:

> On 19/11/2007, Francesco Pietra <[EMAIL PROTECTED]> wrote:
>>
>> How to insert "TER" records recursively, i.e. some thousand fold,  in a
>> file
>> like in the following example? "H2 WAT" is the only constant
>> characteristic of
>> the line after which to insert "TER"; that distinguishes also for lines
>
> If every molecule is water, and therefore 3 atoms, you can use this fact  
> to
> insert TER in the right place. You don't need recursion:
>
> f = open( "atoms.txt", "rt" )
> lineCount = 0
> for line in f.xreadlines( ):
>     lineCount = lineCount + 1
>     print line
>     if lineCount == 3:
>         lineCount = 0
>         print "TER"
> f.close( )

A small variation can handle the original, more generic condition "insert  
TER after the line containing H2
WAT"

f = open("atoms.txt", "r")
for line in f:
     print line
     if "H2  WAT" in line:
         print "TER"
f.close()

(also, note that unless you're using Python 2.2 or earlier, the xreadlines  
call does no good)

-- 
Gabriel Genellina

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to