[英]How to correct one unicode string like “\u8bf8\u845b\u4eae” in python?
I don't know how they printed strings like "\诸\葛\亮" to the log files. 我不知道它们如何在日志文件中打印“ \\ u8bf8 \\ u845b \\ u4eae”之类的字符串。 But now i need to get the correct string from it, it is "诸葛亮" in chinese. 但是现在我需要从中获取正确的字符串,它是中文的“诸葛亮”。 I have tried many ways, but failed. 我尝试了很多方法,但是失败了。
In [56]: print u"\u8bf8\u845b\u4eae"
诸葛亮
In [57]: print "\u8bf8\u845b\u4eae"
\u8bf8\u845b\u4eae
In [58]: "\u8bf8\u845b\u4eae".decode('utf-8')
Out[58]: u'\\u8bf8\\u845b\\u4eae'
In [64]: eval("\u8bf8\u845b\u4eae")
File "<string>", line 1
\u8bf8\u845b\u4eae
^
SyntaxError: unexpected character after line continuation character
You have to use: unicode-escape
您必须使用: unicode-escape
print '\\u8bf8\\u845b\\u4eae'.encode('ascii').decode('unicode-escape')
also see: Converting Unicode string to Chinese characters 另请参阅: 将Unicode字符串转换为汉字
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.