Insert string into string

Roy Smith roy at panix.com
Sat Jul 26 21:25:09 EDT 2008


In article <mailman.714.1217108428.922.python-list at python.org>,
 "Francesco Pietra" <chiendarret at gmail.com> wrote:

> 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)

You're opening "xxx.pdb" for writing, but then not writing to it.  You're 
writing to stdout.

BTW, you might want to take a look at http://biopython.org.



More information about the Python-list mailing list