s = '伟大的中国梦'
#编码str --> bytes
scode_gbk=s.encode(errors='replace') #默认是utf-8,因为utf-8中文占3个字节
print(scode_gbk)
scode_gbk=s.encode('gbk',errors='replace')#gbk中中文占2个字节
print(scode_gbk)
#编码出错问题
s2='耶😊'
scode=s2.encode('gbk',errors='ignore') #strict/ignore/replace
print(scode)
#解码bytes --> str
print(bytes.decode(scode_gbk,'gbk'))
print(bytes.decode(scode,'utf-8'))
字符串的解码与编码
发布于 2024-11-16 12 次阅读
Comments NOTHING