×
Python3 教程Python3 基础语法Python3 基本数据类型Python3 解释器Python3 注释Python3 运算符Python3 数字(Number)Python3 字符串Python3 列表Python3 元组Python3 字典Python3 编程第一步Python3 条件控制Python3 循环语句Python3 迭代器与生成器Python3 函数Python3 数据结构Python3 模块Python3 输入和输出Python3 FilePython3 OSPython3 错误和异常Python3 面向对象Python3 标准库概览Python3 实例

Python3 高级教程

Python3 正则表达式Python3 CGI编程Python3 MySQLPython3 网络编程Python3 SMTP发送邮件Python3 多线程Python3 XML解析Python3 JSONPython3 日期和时间Python3 内置函数Python MongoDBPython uWSGI 安装配置

Python3 bytes.decode()方法


Python3 字符串Python3 字符串


描述

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'\xe8\x8f\x9c\xe9\xb8\x9f\xe6\x95\x99\xe7\xa8\x8b'
GBK 编码: b'\xb2\xcb\xc4\xf1\xbd\xcc\xb3\xcc'
UTF-8 解码: 外唐教程网
GBK 解码: 外唐教程网

Python3 字符串Python3 字符串


分类导航

关注微信下载离线手册

bootwiki移动版 bootwiki
(群号:472910771)