LuissRicardo
LuissRicardo

Reputation: 167

Python (2.7) prints hexadecimal instead special characters

I don't know what happened but I was working with reading files and processing XML with SAX. One '€' character caused an exception so I was trying with decode/encode.

The issue is that before I could do this:

>>> line = '€'
>>> line
'€'

And now it works like this:

>>> line = '€'
>>> line
'\xe2\x82\xac'

This is for all special characters, like: á, é, í, and so on.

How can I solve it?

Upvotes: 0

Views: 78

Answers (1)

Jamerson
Jamerson

Reputation: 484

Use the unicode type:

>>> line = u'€'
>>> print(line)
€

When dealing with character sets, it is always important to know which codec your are decoding from, and encoding to.

For reference, I'm using Python 2.7.8.

Upvotes: 1

Related Questions