Insert character at a fixed position of lines

Lie Ryan lie.1296 at gmail.com
Sat Jul 26 19:26:04 EDT 2008


On Sat, 2008-07-26 at 17:47 +0200, Francesco Pietra wrote:
> Sorry to come again for the same problem. On commanding:
> 
> $ python script.py 2>&1 | tee fileout.pdb
> 
> nothing occurred (fileout.pdb was zero byte). The script reads:
> 
> f = open("xxx.pdb", "w")
> f.write('line = line[:22] + "A" + line[23:]')
> f.close()
> 
> File xxx.pdb is opened by the command: when I forgot the single quote
> ' after [23:] the error answer was:
> SynthaxError: EOL while scanning single-quoted string.
> 
> Thanks
> francesco

I'd expect fileout.pdb to be zero bytes, what you have piped to tee
fileout.pdb is the stdout of your script, i.e. whatever you "print".
Since you "print"ed nothing, nothing is written to fileout.pdb 

You'd find your written material in the file referenced by f, i.e.
"xxx.pdb". 

Btw, if you do f.write('line = line[:22] + "A" + line[23:]'), you'd
output exactly that, and not inserting the 23rd character, you'd want to
do this instead: f.write(line = line[:22] + "A" + line[23:])

> On Sat, Jul 26, 2008 at 1:10 PM, Lie <Lie.1296 at gmail.com> wrote:
> > On Jul 26, 5:42 pm, "Francesco Pietra" <chiendar... at gmail.com> wrote:
> >> I am still at the stone age, using scripts (e.g., to insert a string
> >> after a string) of the type
> >>
> >> f = open("xxx.pdb", "r")
> >> for line in f:
> >>    print line
> >>    if "H16Z POPC" in line:
> >>        print "TER"
> >> f.close()
> >>
> >> That is, I have to learn about modules. In your scripts I am lost
> >> about the filename for the pdb file to modify,
> >
> > In Python, stdin and stdout (as provided by the sys module) is a file-
> > like object, i.e. it have similar behavior as regular files you opened
> > with open(). stdin is a read-only file, while stdout is a write-only
> > file. You already know how to make read-only file, f = open("xxx.pdb",
> > "r"), to make it writable, you've got to use 'w' as the mode: f =
> > open("xxx.pdb", "w")
> >
> > After that you can do this:
> > f.write('some string')
> >
> >
> >> francesco
> >>
> >>
> >>
> >> On Sat, Jul 26, 2008 at 11:35 AM, Lie <Lie.1... at gmail.com> wrote:
> >> > On Jul 26, 2:41 pm, "Francesco Pietra" <chiendar... at gmail.com> wrote:
> >> >> How to insert letter "A" on each line (of a very long list of lines)
> >> >> 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"ATOM" is constant as to both position and string, while
> >> >> "LEU" is constant as to position only, i.e., "LEU" may be replaced by
> >> >> three different uppercase letters. Therefore, the most direct
> >> >> indication would be position 22.
> >>
> >> >> Should the script introduce blank lines, no problem. That I know how
> >> >> to correct with a subsequent script.
> >>
> >> >> Thanks
> >> >> chiendarret
> >>
> >> > If you want to leave the rest of the strings as-is (i.e. the letter A
> >> > overwrites whatever on position 22), Peter's code need to be modified
> >> > a little:
> >> > line = line[:22] + " " + line[23:]
> >> > --
> >> >http://mail.python.org/mailman/listinfo/python-list
> >>
> >> --
> >> Dr Francesco Pietra
> >> Professor of Chemistry
> >> Accademia Lucchese di Scienze, Lettere e Arti, founded in 1594
> >> Palazzo Ducale
> >> 55100 Lucca (Italy)
> >> e-mail chiendar... at gmail.com
> >
> > --
> > http://mail.python.org/mailman/listinfo/python-list
> >




More information about the Python-list mailing list