python 字符串格式的unicode编码转中文

 python对于Unicode编码可以使用decode进行转换成中文:

>>> str = b'\xe8\xb4\xb9\xe8\x84\x91\xe5\xad\x90'

>>> str.decode('utf-8')

'费脑子'


如果是字符串类型的Unicode编码没办法直接用decode进行转换:

>>> str ="\\xe8\\xb4\\xb9\\xe8\\x84\\x91\\xe5\\xad\\x90"

>>> str.decode('utf-8')

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

AttributeError: 'str' object has no attribute 'decode'


处理方式:

>>> str = eval("b" + "\"" + str + "\"")

>>> str.decode('utf-8')

'费脑子'

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容