Modify one character in a string

M

mp

X-No-Archive
How do I go about modifying one character in a string elegantly?
In other words, I want a function that will change 'aaaa' to 'aaza',
given the index 2 of the character in the string.

Also, how do I do this when dealing with a file ; which file mode
should I use and what function should I use to modify a single
character once in that file mode?

Thanks
MP
 
P

Paul Rubin

mp said:
How do I go about modifying one character in a string elegantly?
In other words, I want a function that will change 'aaaa' to 'aaza',
given the index 2 of the character in the string.

Ehh, there are various ways, all ugly.
x = 'aaaa'
y = x[:2] + 'z' + x[3:]
is the most direct.
Also, how do I do this when dealing with a file ; which file mode
should I use and what function should I use to modify a single
character once in that file mode?

You mean you want to change a character in a file?

f = open(file, 'r+') # open for reading and writing
f.seek(2) # position at character index 2
f.write('z') # put a 'z' there
f.seek(0) # rewind
print f.read() # get entire contents and see the change

You could also look at the mmap module.
 
K

keirr

mp said:
X-No-Archive
How do I go about modifying one character in a string elegantly?
In other words, I want a function that will change 'aaaa' to 'aaza',
given the index 2 of the character in the string.

Also, how do I do this when dealing with a file ; which file mode
should I use and what function should I use to modify a single
character once in that file mode?

Thanks
MP

Technically you can't - strings are immutable. If you use the StringIO
module then you can
use a common approach between 'strings' in memory, and files, using
seek and write.

All the best,

Keir.
 
R

Roy Smith

mp said:
X-No-Archive
How do I go about modifying one character in a string elegantly?
In other words, I want a function that will change 'aaaa' to 'aaza',
given the index 2 of the character in the string.

Strings are immutable, so you can't change a string. The best you can do
is create a new string from the old one. Something like:
old = 'aaaa'
new = old[:2] + 'z' + old[3:]
print new
aaza

or a few variations on that theme.
Also, how do I do this when dealing with a file ; which file mode
should I use and what function should I use to modify a single
character once in that file mode?

This is a much more complicated question, because it depends on the details
of the operating system you're using. On Unix, you can seek to a specific
place in a file, write a single character, seek to EOF, and you've done an
in-place single character edit of the file. I don't know if that works on
other OS's or not.
 
L

Larry Bates

mp said:
X-No-Archive
How do I go about modifying one character in a string elegantly?
In other words, I want a function that will change 'aaaa' to 'aaza',
given the index 2 of the character in the string.

Also, how do I do this when dealing with a file ; which file mode
should I use and what function should I use to modify a single
character once in that file mode?

Thanks
MP
IMHO the most elegant method is something like:

def switchchar(srcstring, position, character):
b=list(srcstring)
b[2]=character
return ''.join(b)

You should open file in binary 'b' mode. Use .seek() method
to seek to the character you want to replace. use .write() method
to write one character.

-Larry
 
P

Paul Rubin

Larry Bates said:
IMHO the most elegant method is something like:

def switchchar(srcstring, position, character):
b=list(srcstring)
b[2]=character
return ''.join(b)

If the strings or large or you're doing it a lot, the array module is
likely more efficient.
 
M

Marc 'BlackJack' Rintsch

This is a much more complicated question, because it depends on the details
of the operating system you're using. On Unix, you can seek to a specific
place in a file, write a single character, seek to EOF, and you've done an
in-place single character edit of the file.

Why seeking to EOF after writing the byte?

Ciao,
Marc 'BlackJack' Rintsch
 
S

Scott David Daniels

Marc said:
Why seeking to EOF after writing the byte?

Because some operating systems may create an EOF (thus truncating the
file) after a write, close sequence.

--Scott David Daniels
(e-mail address removed)
 

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
474,296
Messages
2,571,535
Members
48,281
Latest member
DaneLxa72

Latest Threads

Top