Insert string into string

Francesco Pietra chiendarret at gmail.com
Sat Jul 26 17:40:00 EDT 2008


I am posting ex novo as it became confusing to me. I take the
opportunity to ask advice for a second problem.

FIRST PROBLEM
For file xxx.pdb, insert letter "A" into each line that starts with
"ATOM". "A" should be inserted at position 22, i.e., one space after
"LEU", leaving all other characters at the same position as in the
original example:


ATOM      1  N   LEU     1     146.615  40.494 103.776  1.00 73.04       1SG   2

In all lines starting with "ATOM", "LEU" is constant as to position
only (18-20), i.e., "LEU" may be replaced by
three different uppercase letters. Therefore, the most direct
indication would be position 22. If specifying line starting with
"ATOM" makes complication, forget about that as most lines begin with
"ATOM" so that hand correction will be easy.

Script
f = open("xxx.pdb", "w")
import sys

for line in sys.stdin:
    line = line[:22] + "A" + line[23:]
    sys.stdout.write(line)

destroys the xxxx.pdb file and python exits witha non zero exit status.

The same occurs with script

f = open("hASIC1a.B99990003.pdb", "w")
f.write(' line = line[:22] + "A" + line[23:]')
f.close()

I must have misunderstood the suggestion I received on previous posting.
____________________________________
SECOND PROBLEM
File xxx.pdb above has 426 lines stating with "ATOM", this serial
number occupying positions 7-11, right justified (Thus 1, as in the
line example above, means first line). A second, similar file yyy.pdb
has to be concatenated to xxx.pdb. Before that it should be added of
"A" as above and renumbered at position 7-11, starting from 428 (there
is an intermediate line to add). How should a script look like for
this string insertion into string with recursive +1?


Thanks
francesco



More information about the Python-list mailing list