Here is the code:

# coding: utf-8 string = '\u0434\u0438\u0435\u0442\u0430, \u0434\u0438\u0435\u0442\u0430 \u0434\u043b\u044f \u043f\u043e\u0445\u0443\u0434\u0435\u043d\u0438\u044f' print string 

In the console we get

 D:\Python27\python.exe /test.py \u0434\u0438\u0435\u0442\u0430, \u0434\u0438\u0435\u0442\u0430 \u0434\u043b\u044f \u043f\u043e\u0445\u0443\u0434\u0435\u043d\u0438\u044f 

If you do this

 # coding: utf-8 string = u'\u0434\u0438\u0435\u0442\u0430, \u0434\u0438\u0435\u0442\u0430 \u0434\u043b\u044f \u043f\u043e\u0445\u0443\u0434\u0435\u043d\u0438\u044f' print string \ u0435 \ u0442 \ u0430, \ u0434 \ u0438 \ u0435 \ u0442 \ u0430 \ u0434 \ u043b \ u044f \ u043f \ u043e \ u0445 \ u0443 \ u0434 \ u0435 \ u043d \ u0438 \ u044f' # coding: utf-8 string = u'\u0434\u0438\u0435\u0442\u0430, \u0434\u0438\u0435\u0442\u0430 \u0434\u043b\u044f \u043f\u043e\u0445\u0443\u0434\u0435\u043d\u0438\u044f' print string 

then in the console we get the correct conclusion

 диета, диета для похудения 

But the fact is that I get the variable string from the outside, and I can not convert it in any way as in the second version.

That is, in the end I need a code of this type:

 # coding: utf-8 string = string # здесь манипуляции по преобразованию строки из символов юникода в нормлаьный вид print string 
  • Please make type(string) - moden
  • D: \ Python27 \ python.exe /test.py <type 'str'> - Sergey Borisovich
  • My python 33 makes print('\u0434\u0438') normal Russian text in: IDLE Windows, Windows Console, IDLE Ubuntu, Ubuntu Console. - ReinRaus
  • one
    If I understand correctly - the author in the line (obtained from an external source) is, in fact, the result of repr(unicode_text) . The parser language, in this case, nothing to do with. - drdaeman
  • if the input is json text, then json parser should be used instead of unicode-escape : json.loads(r'"\u0434\u0438\u0435"') - jfs

1 answer 1

If I understood everything correctly, the simplest thing is this:

 >>> print unicode(r'\u0434\u0438\u0435\u0442\u0430', 'unicode-escape') диета 

(In the example “r”, just for the sake of clarity, that nothing is decoded “by itself”, by the language parser.)

  • Thanks, it works print unicode (string, 'unicode-escape')! - Sergey B.