Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting Unicode string to Chinese characters

How can I convert the following Unicode string to Chinese characters?

The string is:

'\\u5982\\u679c\\u6211\\u662f\\u4e00\\u4e2a\\u4ece\\u524d\\u7684\\u54f2\\u4eba\\uff0c\\u6765\\u5230\\u4eca\\u5929\\u7684\\u4e16\\u754c\\uff0c\\u6211\\u4f1a\\u6700\\u6000\\u5ff5\\u4ec0\\u4e48\\uff1f'

And I want it to be:

如果我是一个从前的哲人,来到今天的世界,我会最怀念什么?
like image 919
user2870222 Avatar asked Dec 01 '22 18:12

user2870222


1 Answers

Decode it using unicode-escape will give you what you want.

Python 2.7

>>> print '\\u5982\\u679c\\u6211\\u662f\\u4e00\\u4e2a\\u4ece\\u524d\\u7684\\u54f2\\u4eba\\uff0c\\u6765\\u5230\\u4eca\\u5929\\u7684\\u4e16\\u754c\\uff0c\\u6211\\u4f1a\\u6700\\u6000\\u5ff5\\u4ec0\\u4e48\\uff1f'.decode('unicode-escape')
如果我是一个从前的哲人,来到今天的世界,我会最怀念什么?

Python 3.x

>>> print('\\u5982\\u679c\\u6211\\u662f\\u4e00\\u4e2a\\u4ece\\u524d\\u7684\\u54f2\\u4eba\\uff0c\\u6765\\u5230\\u4eca\\u5929\\u7684\\u4e16\\u754c\\uff0c\\u6211\\u4f1a\\u6700\\u6000\\u5ff5\\u4ec0\\u4e48\\uff1f'.encode('ascii').decode('unicode-escape'))
如果我是一个从前的哲人,来到今天的世界,我会最怀念什么?
>>> print(b'\\u5982\\u679c\\u6211\\u662f\\u4e00\\u4e2a\\u4ece\\u524d\\u7684\\u54f2\\u4eba\\uff0c\\u6765\\u5230\\u4eca\\u5929\\u7684\\u4e16\\u754c\\uff0c\\u6211\\u4f1a\\u6700\\u6000\\u5ff5\\u4ec0\\u4e48\\uff1f'.decode('unicode-escape'))
如果我是一个从前的哲人,来到今天的世界,我会最怀念什么?
like image 125
falsetru Avatar answered Dec 10 '22 15:12

falsetru