Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #81071

Re: python 2.7 and unicode (one more time)

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'string': 0.09; '%s"': 0.09; 'explicitely': 0.09; 'formatting': 0.09; 'gettext': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:2.7': 0.09; 'python': 0.11; 'translation': 0.12; '__future__': 0.16; 'codec': 0.16; 'logger': 0.16; 'non-ascii': 0.16; 'n\xc3\xb6tig': 0.16; 'ordinal': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'stuff.': 0.16; 'subject:unicode': 0.16; 'unicode.': 0.16; 'subject:python': 0.16; 'wrote:': 0.18; 'module': 0.19; 'properly': 0.19; 'translated': 0.19; '>>>': 0.22; 'header:User- Agent:1': 0.23; 'byte': 0.24; 'convenient': 0.24; 'unicode': 0.24; 'header': 0.24; 'question': 0.24; 'logging': 0.26; 'header:X -Complaints-To:1': 0.27; 'fixed': 0.29; 'chris': 0.29; 'generally': 0.29; 'asked': 0.31; 'code': 0.31; 'that.': 0.31; '"",': 0.31; 'python2.7': 0.31; 'file': 0.32; '(most': 0.33; 'subject:time': 0.33; 'problem': 0.35; 'subject: (': 0.35; "can't": 0.35; 'there': 0.35; 'subject:one': 0.36; 'should': 0.36; 'application': 0.37; 'to:addr:python-list': 0.38; 'recent': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'received:org': 0.40; 'how': 0.40; 'skip:u 10': 0.60; 'simple': 0.61; 'subject:more': 0.64; 'more': 0.64; 'silently': 0.84; 'contents.': 0.91; 'whereas': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: python 2.7 and unicode (one more time)
Date Thu, 20 Nov 2014 13:35:17 +0100
Organization None
References <m4kcu8$ta8$1@ger.gmane.org>
Mime-Version 1.0
Content-Type text/plain; charset="UTF-8"
Content-Transfer-Encoding 8Bit
X-Gmane-NNTP-Posting-Host p57bd927e.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.16015.1416486934.18130.python-list@python.org> (permalink)
Lines 58
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1416486934 news.xs4all.nl 2850 [2001:888:2000:d::a6]:33134
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:81071

Show key headers only | View raw


Francis Moreau wrote:

> 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")))

This is only problematic if header is a non-ascii bytestring.

> 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 ?

You don't need to change an all-ascii bytestring to unicode. 
Lo and behold:

>>> "%s %s" % (u"üblich", u"ähnlich")
u'\xfcblich \xe4hnlich'
>>> u"%s %s" % (u"üblich", u"ähnlich")
u'\xfcblich \xe4hnlich'

Only non-ascii bytestrings mean trouble, either noisy

>>> u"%s nötig %s" % (u"üblich", "ähnlich")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0: 
ordinal not in range(128)
>>> "%s nötig %s" % (u"üblich", u"ähnlich")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 4: 
ordinal not in range(128)

or silently until you have to decipher the logfile contents. It's best to 
stay away from them, and the

from __future__ unicode_literals

that Chris mentionend is a convenient way to achieve that.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: python 2.7 and unicode (one more time) Peter Otten <__peter__@web.de> - 2014-11-20 13:35 +0100

csiph-web