python - 包含字节的字符串,如何解码
PHP中文网
PHP中文网 2017-04-18 09:59:41
0
4
397

代码如下:

str1 = '\xB4\xF3\xE5N'

请问在python3中,如何把变量str1转成utf-8的字符串.

原编码gbk。字符串中的内容,是从网页采集的内容中的一段。

PHP中文网
PHP中文网

认证高级PHP讲师

reply all(4)
大家讲道理
>>> str1 = '\xB4\xF3\xE5N'
>>> str1
'´óåN'
>>> bytes(str1,'l1').decode('gbk')
'大錘'
>>> unicode = _
>>> unicode
'大錘'
>>> utf8=unicode.encode('utf8')
>>> utf8
b'\xe5\xa4\xa7\xe9\x8c\x98'
>>> 
PHPzhong
>>> import unicodedata
>>> unicodedata.decomposition(u'\xb4')
'<compat> 0020 0301'

from: http://stackoverflow.com/ques...

伊谢尔伦

x has already been encoded, right? If you want to convert to utf8, you must first know its original encoding method.
Method under 2.7:

str1.decode(原编码).encode('utf8')

As for 3, since I have never used it, I can only search it on Baidu. The result is that str is originally unicode, so just encode it directly

bytes_str1 =str1.encode('utf8')
print(str(bytes_str1,'utf8'))
小葫芦

The answer is:

bytes(str1,'l1')

Thank you for “agreeing and accepting” Children’s Shoes

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template