Insert character at a fixed position of lines

F

Francesco Pietra

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
 
P

Peter Otten

Francesco said:
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.

You insert a string into another one using slices

line = line[:22] + " " + line[22:]

(Python's strings are immutable, so you are not really modifying the old
string but creating a new one)
Should the script introduce blank lines, no problem. That I know how
to correct with a subsequent script.

You are probably printing lines read from a file. These lines already end
with a newline, and print introduces a second one. Use the file's write()
method instead of print to avoid that, e. g.:

import sys

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

Peter
 
L

Lie

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:]
 
F

Francesco Pietra

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,

francesco

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:]



--
Dr Francesco Pietra
Professor of Chemistry
Accademia Lucchese di Scienze, Lettere e Arti, founded in 1594
Palazzo Ducale
55100 Lucca (Italy)
e-mail (e-mail address removed)
 
L

Lie

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



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:]

--
Dr Francesco Pietra
Professor of Chemistry
Accademia Lucchese di Scienze, Lettere e Arti, founded in 1594
Palazzo Ducale
55100 Lucca (Italy)
e-mail (e-mail address removed)
 
F

Francesco Pietra

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



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:]

--
Dr Francesco Pietra
Professor of Chemistry
Accademia Lucchese di Scienze, Lettere e Arti, founded in 1594
Palazzo Ducale
55100 Lucca (Italy)
e-mail (e-mail address removed)
 
L

Lie Ryan

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



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 (e-mail address removed)
 
A

alex23

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

Please check your examples before further confusing an already
confused poster.

You -cannot- assign within a function call. What you're doing there is
claiming line is a keyword argument:
File "<stdin>", line 1
f.write(line = 'this doesn't work')
^
SyntaxError: invalid syntax

What you meant, of course, was:
f.write(line[:22] + "A" + line[23:])
 
A

alex23

Ugh, and in pointing our your inaccurate code I posted my own:
File "<stdin>", line 1
f.write(line = 'this doesn't work')
^
SyntaxError: invalid syntax

That should be:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: write() takes no keyword arguments

Sorry about that :)
 
C

castironpi

Ugh, and in pointing our your inaccurate code I posted my own:



That should be:


Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: write() takes no keyword arguments

Sorry about that :)

This is close.

import os
size= os.path.getsize( 'test line_insertion.txt' )
f= open( 'test line_insertion.txt', 'r+' )
linelen= len( f.readline( ) )
f.seek( 20, os.SEEK_SET )
while f.tell( )< size:
f.write( 'y' )
f.seek( linelen, os.SEEK_CUR )
f.flush( )
f.close( )

It assumes lines are a constant length (big assumption), and skips one
line's length of characters starting from the 20th character. It
repeats until current file position is past the original length of the
file.
 
L

Lie

Ugh, and in pointing our your inaccurate code I posted my own:



That should be:


Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: write() takes no keyword arguments

Sorry about that :)

Lessons learned, should test codes even if you thought it seemed
trivial.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top