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


Groups > comp.lang.python > #81065 > unrolled thread

python 2.7 and unicode (one more time)

Started byFrancis Moreau <francis.moro@gmail.com>
First post2014-11-20 10:40 +0100
Last post2014-12-02 14:40 -0800
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  python 2.7 and unicode (one more time) Francis Moreau <francis.moro@gmail.com> - 2014-11-20 10:40 +0100
    Re: python 2.7 and unicode (one more time) Simon Evans <musicalhacksaw@yahoo.co.uk> - 2014-12-02 14:40 -0800

#81065 — python 2.7 and unicode (one more time)

FromFrancis Moreau <francis.moro@gmail.com>
Date2014-11-20 10:40 +0100
Subjectpython 2.7 and unicode (one more time)
Message-ID<mailman.16010.1416476438.18130.python-list@python.org>
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.

[toc] | [next] | [standalone]


#81818

FromSimon Evans <musicalhacksaw@yahoo.co.uk>
Date2014-12-02 14:40 -0800
Message-ID<401fd34b-4671-455a-8496-a3aa9b6af74b@googlegroups.com>
In reply to#81065
Hi Peter Otten
re:
----------------------------------------------------------------------------
There is no assignment 

soup_atag = whatever 

but there is one to atag. The whole session should when you omit the 
offending line 

> atag = soup_atag.a 

or insert 

soup_atag = soup 

before it. 
----------------------------------------------------------------------------
Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib2
>>> from bs4 import BeautifulSoup
>>> html_atag = """<html><body><p>Test html a tag example</p>
... <a href="http://www.packtpub.com'>Home</a>
... <a href="http;//www.packtpub.com/books'.Books</a>
... </body>
... </html>"""
>>> soup = BeautifulSoup(html_atag,'lxml')
>>> atag = soup.aprint(atag)
>>> atag = soup.a
>>> print(atag)
<a href="http://www.packtpub.com'&gt;Home&lt;/a&gt;
&lt;a href=" http="">
</a>
>>> type(atag)
<class 'bs4.element.Tag'>
>>> tagname = atag.name
>>> print tagname
a
>>> atag.name = 'p'
>>> print (soup)
<html><body><p>Test html a tag example</p>
<p href="http://www.packtpub.com'&gt;Home&lt;/a&gt;
&lt;a href=" http="">
</p></body>
</html>
>>> atag.name = 'p'
>>> print(soup)
<html><body><p>Test html a tag example</p>
<p href="http://www.packtpub.com'&gt;Home&lt;/a&gt;
&lt;a href=" http="">
</p></body>
</html>
>>> atag.name = 'a'
>>> print(soup)
<html><body><p>Test html a tag example</p>
<a href="http://www.packtpub.com'&gt;Home&lt;/a&gt;
&lt;a href=" http="">
</a></body>
</html>
>>> soup_atag = soup
>>> atag = soup_atag.a
>>> print (atag['href'])
http://www.packtpub.com'>Home</a>
<a href=
>>>
----------------------------------------------------------------------------
Thank you.
Yours
Simon.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web