gpt4 book ai didi

Python:将unicode字符转换为对应的Unicode字符串

转载 作者:行者123 更新时间:2023-12-05 04:48:57 25 4
gpt4 key购买 nike

如何在 Python 中将 unicode 字符 'ב' 转换为对应的 Unicode 字符串 '\u05d1'?

我前几天问了相反的问题: Python: convert unicode string to corresponding Unicode character

最佳答案

你可以这样做,

>>> x
'ב'
>>> x.encode('ascii', 'backslashreplace').decode('utf-8')
'\\u05d1'

来自docs :

The errors parameter is the same as the parameter of the decode()method but supports a few more possible handlers. As well as 'strict','ignore', and 'replace' (which in this case inserts a question markinstead of the unencodable character), there is also'xmlcharrefreplace' (inserts an XML character reference),backslashreplace (inserts a \uNNNN escape sequence) and namereplace(inserts a \N{...} escape sequence).

关于Python:将unicode字符转换为对应的Unicode字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67763031/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com