Python3教程
+ -

Python3 bytes.decode()方法

2019-09-09 3 0

decode() 方法以指定的编码格式解码 bytes 对象。默认编码为 ‘utf-8’。

语法

decode()方法语法:

bytes.decode(encoding="utf-8", errors="strict")

参数

  • encoding — 要使用的编码,如”UTF-8”。
  • errors — 设置不同错误的处理方案。默认为 ‘strict’,意为编码错误引起一个UnicodeError。 其他可能得值有 ‘ignore’,’replace’, ‘xmlcharrefreplace’, ‘backslashreplace’ 以及通过 codecs.register_error() 注册的任何值。

返回值

该方法返回解码后的字符串。

实例

以下实例展示了decode()方法的实例:

#!/usr/bin/python3
str = "字节流";
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'))

以上实例输出结果如下:

字节流
UTF-8 编码: b'\xe5\xad\x97\xe8\x8a\x82\xe6\xb5\x81'
GBK 编码: b'\xd7\xd6\xbd\xda\xc1\xf7'
UTF-8 解码: 字节流
GBK 解码: 字节流

0 篇笔记 写笔记

Python3 bytes.decode()方法
decode() 方法以指定的编码格式解码 bytes 对象。默认编码为 ‘utf-8’。语法decode()方法语法:bytes.decode(encoding="utf-8", errors="strict")参数encoding — 要使用的编码,如”U......
取消
感谢您的支持,我会继续努力的!
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

您的支持,是我们前进的动力!