decode() 方法以指定的编码格式解码 bytes 对象。默认编码为 'utf-8'。
decode()方法语法:
bytes.decode(encoding="utf-8", errors="strict")
该方法返回解码后的字符串。
以下实例展示了decode()方法的实例:
#!/usr/bin/python3
str = "IT自习室";
str_utf8 = str.encode("UTF-8")
str_gbk = str.encode("GBK")
print(str)
print("UTF-8 编码:", str_utf8)
print("GBK 编码:", str_gbk)
print("UTF-8 解码:", str_utf8.decode('UTF-8','strict'))
print("GBK 解码:", str_gbk.decode('GBK','strict'))
以上实例输出结果如下:
IT自习室
UTF-8 编码: b'IT\xe8\x87\xaa\xe4\xb9\xa0\xe5\xae\xa4'
GBK 编码: b'IT\xd7\xd4\xcf\xb0\xca\xd2'
UTF-8 解码: IT自习室
GBK 解码: IT自习室
©2020 IT自习室京ICP备20010815号