Gio*_*dze 3 python string unicode encoding utf-8
Consider I have a Unicode string (Not the real unicode but the string that looks like unicode). and I want to get it's utf-8 variant. How can I do it in Python? For example If I have String like:
title = "\\u10d8\\u10e1\\u10e0\\u10d0\\u10d4\\u10da\\u10d8 == \\u10d8\\u10d4\\u10e0\\u10e3\\u10e1\\u10d0\\u10da\\u10d8\\u10db\\u10d8"
Run Code Online (Sandbox Code Playgroud)
How Can I do it so that I get its utf-8 variant (Georgian symbols):
??????? == ??????????
To say it simply I want to Have code like:
title = "\\u10d8\\u10e1\\u10e0\\u10d0\\u10d4\\u10da\\u10d8 == \\u10d8\\u10d4\\u10e0\\u10e3\\u10e1\\u10d0\\u10da\\u10d8\\u10db\\u10d8"
utfTitle = title.TurnToUTF()
print(utfTitle)
Run Code Online (Sandbox Code Playgroud)
And I want this code to have output:
??????? == ??????????
干得好。只需使用decode
方法并应用unicode_escape
对于Python 2.x
\n\ntitle = "\\\\u10d8\\\\u10e1\\\\u10e0\\\\u10d0\\\\u10d4\\\\u10da\\\\u10d8 == \\\\u10d8\\\\u10d4\\\\u10e0\\\\u10e3\\\\u10e1\\\\u10d0\\\\u10da\\\\u10d8\\\\u10db\\\\u10d8"\nutfTitle = title.decode(\'unicode_escape\')\nprint(utfTitle)\n\n#output :\xe1\x83\x98\xe1\x83\xa1\xe1\x83\xa0\xe1\x83\x90\xe1\x83\x94\xe1\x83\x9a\xe1\x83\x98 == \xe1\x83\x98\xe1\x83\x94\xe1\x83\xa0\xe1\x83\xa3\xe1\x83\xa1\xe1\x83\x90\xe1\x83\x9a\xe1\x83\x98\xe1\x83\x9b\xe1\x83\x98\n
Run Code Online (Sandbox Code Playgroud)\n\n对于Python 3.x
\n\ntitle = "\\\\u10d8\\\\u10e1\\\\u10e0\\\\u10d0\\\\u10d4\\\\u10da\\\\u10d8 == \\\\u10d8\\\\u10d4\\\\u10e0\\\\u10e3\\\\u10e1\\\\u10d0\\\\u10da\\\\u10d8\\\\u10db\\\\u10d8"\nprint(title.encode(\'ascii\').decode(\'unicode-escape\'))\n
Run Code Online (Sandbox Code Playgroud)\n
您可以使用unicode-escape编解码器摆脱双反斜杠,并有效地使用字符串。
假设title
是a str
,则需要先对字符串进行编码,然后再解码回unicode(str
)。
>>> t = title.encode('utf-8').decode('unicode-escape')
>>> t
'??????? == ??????????'
Run Code Online (Sandbox Code Playgroud)
如果title
是bytes
实例,则可以直接解码:
>>> t = title.decode('unicode-escape')
>>> t
'??????? == ??????????'
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
9039 次 |
最近记录: |