Python怎么将16进制字符串转换成汉字输出呢,python进制,就是这样:In [1]:


就是这样:

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'的字符串,然后输出'中文'呢?

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

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

编橙之家文章,

评论关闭