[python3将str解码为utf8

问题描述 投票:0回答:1

我在python3.6中有一个str变量,如下所示:

\xc3\xa4\xc2\xb8\xc2\xad\xc3\xa5\xc2\x9b\xc2\xbd\xc3\xa6\xc2\xb0\xc2\x91\xc3\xa7\xc2\x94\xc2\x9f\xc3\xa9\xc2\x93\xc2\xb6\xc3\xa8\xc2\xa1\xc2\x8c

我想将str解码为中文,我先对str进行编码,然后对其进行解码,但是它不起作用,我的代码如下:

str = '\xE4\xB8\xAD\xE5\x9B\xBD\xE6\xB0\x91\xE7\x94\x9F\xE9\x93\xB6\xE8\xA1\x8C'
str.encode('utf-8').decode('unicode_escape')

输出如下:

ä¸Â\xadÃ¥Â\x9b½æ°Â\x91çÂ\x94Â\x9féÂ\x93¶è¡Â\x8c
python-3.x decode encode
1个回答
0
投票
>>> s = '\xE4\xB8\xAD\xE5\x9B\xBD\xE6\xB0\x91\xE7\x94\x9F\xE9\x93\xB6\xE8\xA1\x8C' >>> s.encode('latin-1').decode('utf-8') '中国民生银行'

我听不懂中文,但是Google翻译认为它写着“中国民生银行”。输出有意义吗?

© www.soinside.com 2019 - 2024. All rights reserved.