Mapping with unicode strings buggy the format operator %

Chema Cortes chemacortes at
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
>>> 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'

Chema Cortes (chemacortes at
    Linux User #142755 - SuSE Linux 7.1

More information about the Python-list mailing list