python 2.7 and unicode (one more time)

Francis Moreau francis.moro at gmail.com
Thu Nov 20 04:40:24 EST 2014


Hello,

My application is using gettext module to do the translation
stuff. Translated messages are unicode on both python 2 and
3 (with python2.7 I had to explicitely asked for unicode).

A problem arises when formatting those messages before logging
them. For example:

  log.debug("%s: %s" % (header, _("will return an unicode string")))

Indeed on python2.7, "%s: %s" is 'str' whereas _() returns
unicode.

My question is: how should this be fixed properly ?

A simple solution would be to force all strings passed to the
logger to be unicode:

  log.debug(u"%s: %s" % ...)

and more generally force all string in my code to be unicode by
using the 'u' prefix.

or is there a proper solution ?

Thanks.




More information about the Python-list mailing list