簡體   English   中英

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

[英]How to correct one unicode string like “\u8bf8\u845b\u4eae” in python?

我不知道它們如何在日志文件中打印“ \\ u8bf8 \\ u845b \\ u4eae”之類的字符串。 但是現在我需要從中獲取正確的字符串,它是中文的“諸葛亮”。 我嘗試了很多方法,但是失敗了。

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

您必須使用: unicode-escape

print '\\u8bf8\\u845b\\u4eae'.encode('ascii').decode('unicode-escape')

另請參閱: 將Unicode字符串轉換為漢字

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM