Ulyarez
Ulyarez

Reputation: 155

Behaviour unicode string in python

I have seen this question I have doubts about how can I convert a var to unicode on running time ? Is it right use unicode function ? Are there other way to convert a string on running time ?

print(u'Cami\u00f3n') # prints with right special char

name=unicode('Cami\u00f3n')
print(name) # prints bad ===> Cami\u00f3n

name.encode('latin1')
print(name.decode('latin1')) # prints bad ===> Cami\u00f3n

encoded_id = u'abcd\xc3\x9f'
encoded_id.encode('latin1').decode('utf8')
print encoded_id.encode('latin1').decode('utf8') # prints right

I saw a lot of python unicode questions on stackoverflow but i can't understand this behaviour.

Upvotes: 2

Views: 478

Answers (1)

Kasravnd
Kasravnd

Reputation: 107287

Its just because of that if you don't specify any encoding for unicode function then :

unicode() will mimic the behaviour of str() except that it returns Unicode strings instead of 8-bit strings. More precisely, if object is a Unicode string or subclass it will return that Unicode string without any additional decoding applied.

So you'll have a str version of your unicode (the Unicode part will be escaped):

>>> name=unicode('Cami\u00f3n')
>>> print(name)
Cami\u00f3n
>>> name
u'Cami\\u00f3n'
       ^ 

For get ride of this problem you can use 'unicode-escape' as your encoding to escape converting the Unicode to string!

>>> name=unicode('Cami\u00f3n','unicode-escape')
>>> name
u'Cami\xf3n'
>>> print(name)
Camión

Upvotes: 6

Related Questions