6
votes

I have unicode string, i'm sure that it's UTF-8, but I can't decode it. The string is '\u041b\u0435\u0433\u043a\u043e\u0432\u044b\u0435'. How to decode it?

1
decode to what - how do you want to use it? - mmmmmm
Oh, I'm dumb :) print u'\u0441\u0438\u0434\u0435\u043d\u044c\u044f' gives normal Human-readable string. - artem
It is definitely not UTF8. - glglgl
@glglgl, which encoding can it be? - artem
@RankoR None at all - it is just Unicode. \u serves to escape a unicode codepoint within unicode strings (u'' in 2.x, '' in 3.x). If they occur within a string (2.x) resp. bytes object (3.x), they can be decoded as shown in the answer below. - glglgl

1 Answers

28
votes

You can use aString.decode('unicode_escape'), it convert a unicode-format string to unicode object

>>> u'\u041b\u0435\u0433\u043a\u043e\u0432\u044b\u0435'
u'\u041b\u0435\u0433\u043a\u043e\u0432\u044b\u0435'
>>> '\u041b\u0435\u0433\u043a\u043e\u0432\u044b\u0435'.decode('unicode_escape')
u'\u041b\u0435\u0433\u043a\u043e\u0432\u044b\u0435'
>>>

In your case

>>> print '\u041b\u0435\u0433\u043a\u043e\u0432\u044b\u0435'.decode('unicode_escape')
Легковые
>>>