Mapping with unicode strings buggy the format operator %
Martin von Loewis
loewis at informatik.hu-berlin.de
Sun Jul 1 07:20:14 EDT 2001
chemacortes at wanadoo.WIPE_ME.es (Chema Cortes) writes:
> I have a dictionary with unicode strings as keys. Formating it with the
> format operator % raise errors when the key cannot convert into a
> non-unicode string. I try to surround the error, but the only thing I
> can do is not to use unicode keys in dictionaries. I supose that bug is
> caused because the format operator use the re module, not the sre (with
> unicode support).
No, it's a different problem: for %(name)s arguments in Unicode
strings, name is converted to UTF-8; the resulting UTF-8 string is
then searched in the dictionary.
> >>> d={ "año":2001 }
> >>> "%(año)4d" % d
> '2001'
> >>> d={ u"año":2001 }
> >>> "%(año)4d" % d # KeyError: año
This fails because u"año" and "año" cannot be compared: byte strings
and Unicode strings are comparable only if the byte string is ASCII.
> >>> u"%(año)4d" % d # KeyError: año
This fails because the key is converted to UTF-8 before lookup.
I recommend to report a bug to sf.net/projects/python.
Regards,
Martin
More information about the Python-list
mailing list