代码如下:
str1 = '\xB4\xF3\xE5N'
请问在python3中,如何把变量str1转成utf-8的字符串.
原编码gbk。字符串中的内容,是从网页采集的内容中的一段。
认证高级PHP讲师
>>> 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' >>>
>>> 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
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:
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
The answer is:
Thank you for “agreeing and accepting” Children’s Shoes