string.translate with unicode
bearophileHUGS at lycos.com
bearophileHUGS at lycos.com
Sun Aug 6 10:23:29 EDT 2006
peterbe at gmail.com:
It's not a bug, but such incompatibility problem will probably be
solved with Python 3.0, when most strings will managed as unicode.
The documentation says:
>it returns a copy of the s where all characters have been mapped through the given translation table which must be a mapping of Unicode ordinals to Unicode ordinals, Unicode strings or None. Unmapped characters are left untouched. Characters mapped to None are deleted.<
An example:
def maketransU(s1, s2, todel=""):
trans_tab = dict( zip( map(ord, s1), map(ord, s2) ) )
trans_tab.update( (ord(c),None) for c in todel )
return trans_tab
trans_tab_u = maketransU('_', ' ', "?&!;<=>*#[]{}")
print u"Peter!_*fine*".translate(trans_tab_u)
Bye,
bearophile
More information about the Python-list
mailing list