[python] 如何将unicode字符串转换为中文

时间:2021-09-08 07:43:37

答案:(http://*.com/) ps:这个网站解决了我好多问题啊,大家多上

>>>s='\u9648\u4f1f\u9706\u5176\u5b9e\u662f\u4e2a'
>>>print s.decode('unicode-escape')
>>>陈伟霆其实是个

  

首先你有类似这样的字符串:

\u9648\u4f1f\u9706\u5176\u5b9e\u662f\u4e2a

于是你尝试直接打印,希望能变成中文

>>>s = '\u9648\u4f1f\u9706\u5176\u5b9e\u662f\u4e2a'
>>>print s
>>>\u9648\u4f1f\u9706\u5176\u5b9e\u662f\u4e2a

发现没用,于是又尝试

>>>s = '\u9648\u4f1f\u9706\u5176\u5b9e\u662f\u4e2a'
>>>us = unicode(s,'utf8')
>>>print us
>>>'\\u9648\\u4f1f\\u9706\\u5176\\u5b9e\\u662f\\u4e2a'

还是不行,反斜杠被转意了。然后就抓狂,艹艹艹艹艹艹艹艹艹艹艹艹艹艹艹艹艹艹艹艹艹艹艹艹艹艹艹艹 艹!!! 

最后再感叹谷歌的好,*的好