By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,705 Members | 1,216 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,705 IT Pros & Developers. It's quick & easy.

Insert character at a fixed position of lines

P: n/a
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
Jul 26 '08 #1
Share this Question
Share on Google+
7 Replies

P: n/a
Francesco Pietra 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.
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
Jul 26 '08 #2

P: n/a
Lie
On Jul 26, 2:41*pm, "Francesco Pietra" <chiendar...@gmail.comwrote:
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:]
Jul 26 '08 #3

P: n/a
On Jul 27, 9:26 am, Lie Ryan <lie.1...@gmail.comwrote:
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:
>>f = open('dummy.txt','w')
f.write(line = 'this doesn't work')
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:])
Jul 27 '08 #4

P: n/a
Ugh, and in pointing our your inaccurate code I posted my own:
>f = open('dummy.txt','w')
f.write(line = 'this doesn't work')

File "<stdin>", line 1
f.write(line = 'this doesn't work')
^
SyntaxError: invalid syntax
That should be:
>>f.write(line = "this doesn't work")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: write() takes no keyword arguments

Sorry about that :)
Jul 27 '08 #5

P: n/a
On Jul 26, 10:02*pm, alex23 <wuwe...@gmail.comwrote:
Ugh, and in pointing our your inaccurate code I posted my own:
>>f = open('dummy.txt','w')
>>f.write(line = 'this doesn't work')
* File "<stdin>", line 1
* * f.write(line = 'this doesn't work')
* * * * * * * * * * * * * * * *^
SyntaxError: invalid syntax

That should be:
>f.write(line = "this doesn't work")

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.
Jul 27 '08 #6

P: n/a
Lie
On Jul 27, 10:02*am, alex23 <wuwe...@gmail.comwrote:
Ugh, and in pointing our your inaccurate code I posted my own:
>>f = open('dummy.txt','w')
>>f.write(line = 'this doesn't work')
* File "<stdin>", line 1
* * f.write(line = 'this doesn't work')
* * * * * * * * * * * * * * * *^
SyntaxError: invalid syntax

That should be:
>f.write(line = "this doesn't work")

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.
Jul 30 '08 #7

P: n/a
On Jul 30, 10:43*pm, Lie <Lie.1...@gmail.comwrote:
Lessons learned, should test codes even if you thought it seemed
trivial.
And I learned I should always make sure to cut&paste the right
example :)
Jul 31 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.