unicode - 如何用python将utf8编码的16进制字符串转换成对应的文字?
ringa_lee
ringa_lee 2017-04-17 13:36:36
[Python讨论组]

就是这样:

In [1]: '中文'.decode('utf8')
Out[1]: u'\u4e2d\u6587'

In [2]: u'\u4e2d\u6587'.encode('utf8')
Out[2]: '\xe4\xb8\xad\xe6\x96\x87'

In [3]: unicode('\u4e2d\u6587')
Out[3]: u'\\u4e2d\\u6587'

好了,问题是,如何在python下,接收类似'\u4e2d\u6587'的字符串,然后输出'中文'呢?

ringa_lee
ringa_lee

ringa_lee

全部回复(2)
ringa_lee

For Python3

b'\u4e2d\u6587'.decode('unicode-escape')
请参阅https://docs.python.org/3.4/library/codecs.html#text-encodings

For Python2

'\u4e2d\u6587'.decode('unicode-escape') (你可能需要print它才能看到结果)
请参阅https://docs.python.org/2.7/library/codecs.html#python-specific-encodings

PHP中文网

print u'\u4e2d\u6587'.encode('utf8')

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号