On 19/11/2007, <b class="gmail_sendername">Francesco Pietra</b> <<a href="mailto:chiendarret@yahoo.com">chiendarret@yahoo.com</a>> wrote:<div><span class="gmail_quote"></span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
New to the list and just beginning with Python (Linux B console). Urgent<br>problem before I can correctly program:<br><br>How to insert "TER" records recursively, i.e. some thousand fold,  in a file<br>like in the following example? "H2 WAT" is the only constant characteristic of
<br>the line after which to insert "TER"; that distinguishes also for lines for<br>other atoms. Just to illustrate what I want to do - for those unfamiliar with<br>this type of file - a three-line set between two "TER" defines a water
<br>molecule, with a set of xyz coordinates for each atom.<br></blockquote></div><br>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:
<br><br>f = open( "atoms.txt", "rt" )<br>lineCount = 0<br>for line in f.xreadlines( ):<br>    lineCount = lineCount + 1<br>    print line<br>    if lineCount == 3:<br>        lineCount = 0<br>        print "TER"
<br>f.close( )<br><br>You could do the above in fewer lines with a regex (this would also work even if all molecules weren't water), which would probably be neater, but is left as an exercise for the reader :) There are probably much better idiomatic ways to do this, and I defer to much more experienced Python programmers to show you them.
<br><br>HTH,<br><br>Henry<br>