How to decode double backlash string in python?

Viewed 93

The input string is string = '\\u56db\\u5ddd\\u9ec4\\u9f99\\u590d\\u534e\\u5ea6\\u5047\\u4e16\\u754c'. Note that it is double-slash, not single slash.

How to use decode it with gbk encoding to get 四川黄龙复华度假世界?

2 Answers

Here, a simple replace method won't work. The simplest way is to do the following:

from codecs import unicode_escape_decode
text = '\\u56db\\u5ddd\\u9ec4\\u9f99\\u590d\\u534e\\u5ea6\\u5047\\u4e16\\u754c'
decoded_text = unicode_escape_decode(text)
print(decoded_text)

Output: 四川黄龙复华度假世界

The following code snippet works in Python 3.8.6 … on win32:

texte = '\\u56db\\u5ddd\\u9ec4\\u9f99\\u590d\\u534e\\u5ea6\\u5047\\u4e16\\u754c'
textd = repr(texte).replace('\\\\','\\').strip("'").encode().decode("unicode-escape")
print(textd)
四川黄龙复华度假世界
Related