Mapping with unicode strings buggy the format operator %
Chema Cortes
chemacortes at wanadoo.WIPE_ME.es
Thu Jun 28 14:48:58 EDT 2001
Hi to all:
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).
Here is the buggy code:
>>> d={ "año":2001 }
>>> "%(año)4d" % d
'2001'
>>> d={ u"año":2001 }
>>> "%(año)4d" % d # KeyError: año
>>> u"%(año)4d" % d # KeyError: año
But this code is ok:
>>> d={ u"mes": 6}
>>> "%(mes)2d" % d
' 6'
>>> u"%(mes)2d" % d
u' 6'
Thanks,
--
Chema Cortes (chemacortes at wanadoo.es)
Linux User #142755 - SuSE Linux 7.1
ZARALINUX http://zaralinux.org/
More information about the Python-list
mailing list