pdubois
pdubois

Reputation: 7800

Printing bytestring via variable

I have the following Unicode text stored in variable:

 myvariable = 'Gen\xe8ve'

What I want to do is to print myvariable and show this:

Genève

I tried this but failed:

print myvariable.decode('utf-8')

What's the right way to do it? At the end I'd like to print the string into a text file. I'm using Python 2.7.

Update: Also tried this:

In [23]: myvariable = u'Gen\xe8ve'

In [24]: print myvariable
---------------------------------------------------------------------------
UnicodeEncodeError                        Traceback (most recent call last)
<ipython-input-24-1eb59a50889d> in <module>()
----> 1 print myvariable

UnicodeEncodeError: 'ascii' codec can't encode character u'\xe8' in position 3: ordinal not in range(128)

Update 2: I really want to print from myvariable. In actual code Gen\xe8ve are extracted from xml.etree.ElemTree parser, like:

myvariable = actress.find('name').text
## The following doesn't work. 
# print u'myvariable'

Upvotes: 0

Views: 122

Answers (3)

gog
gog

Reputation: 11347

When you print a unicode string directly

myvariable = u'Gen\xe8ve'
print myvariable

python tries to encode it with the default encoding (sys.stdout.encoding). Since it appears to be ascii on your system, it tries ascii and fails (there's no such thing as \xe8 in ascii). Try specifying the encoding explicitly:

myvariable = u'Gen\xe8ve'
print myvariable.encode('utf-8')

Upvotes: 1

RemcoGerlich
RemcoGerlich

Reputation: 31260

'\xe8' isn't UTF8, it's some other encoding.

Try:

>>> x = 'Gen\xc3\xa8ve'
>>> print x.decode('utf8')

Or find out what the encoding actually is, and decode that.

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798744

That's not Unicode text, that's a bytestring. This is Unicode text:

myvariable = u'Gen\xe8ve'
print myvariable

Upvotes: 1

Related Questions