How to modify a string in place?

Andreas Jung ajung at sz-sb.de
Fri May 14 13:28:13 EDT 1999


On Fri, May 14, 1999 at 11:16:12AM -0500, John Powers wrote:
> I have read a large binary image of a program into a Python string and need
> to patch hundreds of places in the string to reflect relocated addresses.
> Since strings are immutable, I wind up cutting the string apart (beforehole
> = image[:hole]; afterhole = image[hole+4:]) then reconstructing the image
> (image = beforehole + reloc + afterhole). This is extremely slow!
> 
> I would like to modify the string in place. Any ideas?
> 

Maybe you should try:

1) convert the string to a list (lst = list(mystr)). Because lists
  are mutable you can change the elements of the lists with the
  known list functions.

2) take look at the array module.

Cheers,
Andreas

-- 
                                _\\|//_
                               (' O-O ')
------------------------------ooO-(_)-Ooo--------------------------------------
   Andreas Jung, Saarbrücker Zeitung Verlag und Druckerei GmbH
   Saarbrücker Daten-Innovations-Center
   Gutenbergstr. 11-23, D-66103 Saarbrücken, Germany
   Phone: +49-(0)681-502-1528, Fax: +49-(0)681-502-1509
   Email: ajung at sz-sb.de (PGP key available)
-------------------------------------------------------------------------------




More information about the Python-list mailing list