Reputation: 315
If I define the variable
x = 'Ááa Éée'
then the output of
print x
is
Ááa Éée
But I have an unicode
object
x = u'Ááa Éée'
and I need the same output as before. To do this, I tried converting it to a str
with
str(u'Ááa Éée')
but it didn't work.
How can I do this? (I'm only interested exit.)
Upvotes: 1
Views: 264
Reputation: 11
x = u'Ááa Éée' is unicode string
so, you You can only use unicode()
instead str()
use unicode string
You need to specify the encoding
string = x.encode('utf-8') #utf-16 or ...
print string.decode('utf-8')
Upvotes: 0
Reputation: 25181
str(u'Ááa Éée')
is not working, because this conversion unicode -> str uses encoding ASCII by default and characters ÁáÉé are not present in ASCII.
You need this: u'Ááa Éée'.encode("UTF-8")
- if your terminal uses UTF-8.
Things about unicode can be complicated, it's better to read something about it:
Upvotes: 2
Reputation: 41878
Actually, print u"Ááa Éée"
should give you the exact same output as print "Ááa Éée"
. Maybe you are confusing with printing the representation of each one on the terminal. Anyway, if what you're asking is how to convert the unicode to str, use x.encode('utf-8')
.
Upvotes: 4