data = “python中文测试”
utf-8编码之后:
data_utf8 = data.encode(“utf-8”)
data = “python中文测试”
gbk编码之后:
data_gbk = data.encode(“gbk”)
python decode类似\xe6\xb5等16进制编码,可以采用如下方式:
def my_decode(value):
try:
msg = value.decode(“utf-8”)
except Exception as e:
msg = value.decode(“gbk”)
except:
msg = “unknow,{}”.format(value)
finally:
return msg
if name == “main”:
data = b’python\xd6\xd0\xce\xc4\xb2\xe2\xca\xd4’
print(my_decode(data))