简体   繁体   English

如何在python中更正一个Unicode字符串,如“ \\ u8bf8 \\ u845b \\ u4eae”?

[英]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.

相关问题 如何将“ \\ u5c0f \\ u738b \\ u5b50 \\ u003a \\ u6c49 \\ u6cd5 \\ u82f1 \\ u5bf9 \\ u7167”这样的字符串转换为汉字 - How can I convert strings like “\u5c0f\u738b\u5b50\u003a\u6c49\u6cd5\u82f1\u5bf9\u7167” to Chinese characters 如何在Python中将u&#39;\\\\ u4f60 \\\\ u4f60&#39;这样的unicode字符串转换为u&#39;\\ u4f60 \\ u4f60&#39;? - How to convert unicode string like u'\\u4f60\\u4f60' to u'\u4f60\u4f60' in Python? Python Unicode 字符串在文件中存储为 &#39;\蒸\汽\地&#39;,如何将其转换回 Unicode? - Python Unicode string stored as '\u84b8\u6c7d\u5730' in file, how to convert it back to Unicode? 如何在 Python 中解码以“%u”(百分号 + u)开头的 unicode 字符串 - How to decode the unicode string starting with “%u” (percent symbol + u) in Python 3 如何在python中的代码点上拆分unicode字符串? (例如\\ u00B7或\\ u2022)? - How do I split a unicode string on code points in python? (eg. \u00B7 or \u2022)? Python unicode规范化:将u&#39;\\ xb4&#39;翻译为u&#39;\\ u0301&#39;是否正确 - Python unicode normalization: is it correct to translate u'\xb4' to u' \u0301' 如何在 Python 2.7 中打印像“u{variable}”这样的 Unicode? - How to print Unicode like “u{variable}” in Python 2.7? Python unicode字符串文字::'\ u0391'和u'\ u0391'之间的区别是什么 - Python unicode string literals :: what's the difference between '\u0391' and u'\u0391' 如何通过在字符串中使用 \\u 或 \\U 转义来正确表示 python3 (3.6.1+) 中的补充 unicode 字符 - How to correctly represent a supplementary unicode char in python3 (3.6.1+) by using \u or \U escape within string Python - unicode字符串中的ASCII编码字符串; 如何删除&#39;你&#39;? - Python - ASCII encoding string in the unicode string; how to remove that 'u'?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM