Replace accented chars with unaccented ones

"Martin v. Löwis" martin at v.loewis.de
Tue Mar 16 15:20:37 EST 2004


Josiah Carlson wrote:
> Translating the replacements pairs into a dictionary would result in a 
> significant speedup for large numbers of replacements.
> 
> mapping = dict(replacement_pairs)
> 
> def multi_replace(inp, mapping=mapping):
>     return u''.join([mapping.get(i, i) for i in inp])

Using the .translate() method on unicode strings should be
even more performant:

# prepare mapping table to match .translate interface
table = {}
for k,v in replacement_pairs: table[ord(k)]=v

def multi_replace(inp):
   return inp.translate(table)

Regards,
Martin




More information about the Python-list mailing list