Python3 interpret unicode literal string as unicode character

怎甘沉沦 提交于 2020-05-30 03:31:25

问题


Now I have some unicode literal string like "\\u0061" which is by default interpreted as 6 unicode character. How can I convert it into unicode character 'a' ?.


回答1:


Even easier:

>>> "\\u0061".encode().decode('unicode-escape')
'a'
>>> 



回答2:


You're looking for the unicode-escape codec:

>>> import codecs
>>> print(r'\u2603')
\u2603
>>> print(codecs.decode(r'\u2603', 'unicode-escape'))
☃


来源:https://stackoverflow.com/questions/52196916/python3-interpret-unicode-literal-string-as-unicode-character

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!