[BangPypers] chaining string operations..

Vishal vsapre80 at gmail.com
Fri Mar 25 10:52:02 CET 2011


I have a string that can be many megabytes worth memory size, and i want to
perform simple string replacements. There is a certain sequence I have to
follow...so I do the following:

>>> type(s)
<type 'str'>
>>> s1 = s.replace(' a', 'S').replace(' \t', '\n').replace('C#', '\n')

Now I would like to chain these replacements into something like this:

>>> s1 = s.replacements((' a', 'S'), (' \t', '\n'), ('C#', '\n')))

One could write this function in Python itself, but I want to push this down
to the C level.

Is there a known way of doing this currently in Python, apart from producing
my own Cython/C extension module.

Thanks and best regards,
Vishal Sapre

More information about the BangPypers mailing list