[Tutor] unicode mapping doesn't work
Lie Ryan
lie.1296 at gmail.com
Thu Nov 26 17:33:15 CET 2009
Albert-Jan Roskam wrote:
> Hi,
>
> I want to substitute some letters with accents with theire non-accented
> equivalents. It should be easy, but it doesn't work. What am I doing wrong?
>
> trans = {}
> funnychars = u"éèêëóòôöáàâäÉÈÊËÓÒÔÖÁÀÂÄ"
> asciichars = "eeeeooooaaaaEEEEOOOOAAAA"
> for f, a in zip(funnychars, asciichars):
> trans.update({f: a})
> print funnychars.translate(trans) # why doesn't this return the letters
> without accents?
>
Perhaps the doc should be more obvious and perhaps unicode.translate is
badly designed, but:
translate(...)
S.translate(table) -> unicode
Return a copy of the string 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.
it says "mapping of Unicode ordinals to Unicode ordinals, Unicode
strings or None"
which reads as a mapping of (int) to (int, unicode, or None).
your translation table is a mapping of (unicode) to (str).
this works:
for f, a in zip(funnychars, asciichars):
trans.update({ord(f): unicode(a)})
this as well:
for f, a in zip(funnychars, asciichars):
trans.update({ord(f): ord(a)})
btw, you're updating the dict and creates a new dict for each character
then immediately disposing it. Why not just:
for f, a in zip(funnychars, asciichars):
trans[ord(f)] = ord(a)
More information about the Tutor
mailing list