Thank you! Quite frankly, I didn't know about ordinals *blushes*.
Also, I thought that d.update({k: v}) was equivalent to d[k] = v, but
apparently it's not. So thank you twice!
Cheers!!
Albert-Jan
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In the face of ambiguity, refuse the temptation to guess.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--- On Thu, 11/26/09, Lie Ryan <[email protected]> wrote:
From: Lie Ryan <[email protected]>
Subject: Re: [Tutor] unicode mapping doesn't work
To: [email protected]
Date: Thursday, November 26, 2009, 5:33 PM
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)
_______________________________________________
Tutor maillist - [email protected]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
_______________________________________________
Tutor maillist - [email protected]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor