String multi-replace

Frederic Rentsch anthra.norell at bluewin.ch
Thu Nov 18 16:38:09 EST 2010


On Wed, 2010-11-17 at 21:12 -0800, Sorin Schwimmer wrote:
> Thanks for your answers.
> 
> Benjamin Kaplan: of course dict is a type... silly me! I'll blame it on the time (it's midnight here).
> 
> Chris Rebert: I'll have a look.
> 
> Thank you both,
> SxN
> 
> 

Forgive me if this is off the track. I haven't followed the thread. I do
have a little module that I believe does what you attempted to do:
multiple substitutions using a regular expression that joins a bunch of
targets with '|' in between. Whether or not you risk unintended
translations as Dave Angel pointed out where the two characters or one
of your targets join coincidentally you will have to determine. If so
you can't use this approach. If, on the other hand, your format is safe
it'll work just fine. Use like this:

>>> import translator
>>> t = translator.Translator (nodia.items ())
>>> t (name)  # Your example
'Rasca'


Frederic



-------------- next part --------------
A non-text attachment was scrubbed...
Name: translator.py
Type: text/x-python
Size: 4276 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20101118/4d849120/attachment-0001.py>


More information about the Python-list mailing list