Python encode() 方法
描述
encode() 方法以指定的编码格式编码字符串,默认编码为 'utf-8'。
对应的解码方法:bytes decode() 方法。
语法
encode() 方法语法:
S.encode([encoding='utf-8'][,errors='strict'])
参数
- encoding -- 可选参数,要使用的编码,默认编码为 'utf-8'。
- errors -- 可选参数,设置不同错误的处理方案。默认为 'strict',意为编码错误引起一个UnicodeError。 其他可能得值有 'ignore', 'replace', 'xmlcharrefreplace', 'backslashreplace' 以及通过 codecs.register_error() 注册的任何值。
返回值
该方法返回编码后的字符串,它是一个 bytes 对象。
实例
以下实例展示了 encode() 方法的实例:
#!/usr/bin/python3 S = "菜鸟教程";
S_utf8 = S.encode("UTF-8")
S_gbk = S.encode("GBK") print(S) print("UTF-8 编码:", S_utf8)
print("GBK 编码:", S_gbk) print("UTF-8 解码:", S_utf8.decode('UTF-8','strict'))
print("GBK 解码:", S_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 解码: 菜鸟教程
Python encode() 方法的更多相关文章
- Python encode() 方法(转)
转自:http://www.cnblogs.com/wushuaishuai/p/7686290.html 描述 encode() 方法以指定的编码格式编码字符串,默认编码为 'utf-8'. 对应的 ...
- 字符编码和python .encode().decode()方法
字符编码与encode.decode的问题: 用8个开关表示世界万物 ASCII : American Standard Code for Information Interchange,美国 ...
- Python 字符串方法详解
Python 字符串方法详解 本文最初发表于赖勇浩(恋花蝶)的博客(http://blog.csdn.net/lanphaday),如蒙转载,敬请保留全文完整,切勿去除本声明和作者信息. ...
- python encode和decode函数说明【转载】
python encode和decode函数说明 字符串编码常用类型:utf-8,gb2312,cp936,gbk等. python中,我们使用decode()和encode()来进行解码和编码 在p ...
- pyhton字符编码问题--decode和encode方法
1 decode和encode方法 字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码,即先将其他编码的字符串解码(decode)成uni ...
- Python String 方法详解
官网文档地址:https://docs.python.org/3/library/stdtypes.html#string-methods 官网 公号:软测小生ruancexiaosheng 文档里的 ...
- Python decode()方法
描述 Python decode() 方法以 encoding 指定的编码格式解码字符串.默认编码为字符串编码.高佣联盟 www.cgewang.com 语法 decode()方法语法: str.de ...
- python encode decode
Python encode()encode() 方法以 encoding 指定的编码格式编码字符串.errors参数可以指定不同的错误处理方案.写法:str.encode(encoding='UTF- ...
- Python swapcase()方法
首先,要明白Python swapcase() 方法用于对字符串的大小写字母进行转换. 其次,了解swapcase()方法语法:str.swapcase() 返回值:返回大小写字母转换后生成的新字符串 ...
随机推荐
- Linux系统Apache服务 - 配置 HTTP 的虚拟机主机
接Linux系统Apache服务 - 配置HTTP的默认主页 1.创建/srv目录,作为httpd的文件目录,并创建/srv/default/www和/srv/www1.example.com/www ...
- 每日英语:Proactive Advice for Dealing With Grief: Seek Out New Experiences
When her husband died of cancer 10 years ago, Becky Aikman says she experienced grief and adapted to ...
- 每日英语:How Your Knees Can Predict the Weather
The Wolff family of Paramus, N.J., was eyeing the gathering clouds and debating whether to cancel a ...
- django中使用POST方法 获取POST数据
在django中获取post数据,首先要规定post发送的数据类型是什么. 1.获取POST中表单键值数据 如果要在django的POST方法中获取表单数据,则在客户端使用JavaScript发送PO ...
- Leetcode:Interleaving String 解题报告
Interleaving StringGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For ...
- C#学习笔记(15)——c#接口
说明(2017-7-17 21:57:26): 原文:http://www.cnblogs.com/jiajiayuan/archive/2011/09/16/2178462.html 本文意在巩固基 ...
- [转]mysql 行转列 列转行
原文地址:http://www.cnblogs.com/xiaoxi/p/7151433.html 一.行转列 即将原本同一列下多行的不同内容作为多个字段,输出对应内容. 建表语句 DROP TABL ...
- PHP写的一个轻量级的DI容器类(转)
理解什么是Di/IoC,依赖注入/控制反转.两者说的是一个东西,是当下流行的一种设计模式.大致的意思就是,准备一个盒子(容器),事先将项目中可能用到的类扔进去,在项目中直接从容器中拿,也就是避免了直接 ...
- Python 创建线程的方法
使用的是 threading 模块 代码如下: 1 #!/usr/bin/python3 2 3 import threading 4 import time 5 6 exitFlag = 0 7 / ...
- 隐藏的Word快捷键操作
原文地址:http://tieba.baidu.com/p/4163778583 原文来自于: 新文咖从会用电脑开始,Microsoft Office Word就是我们最常用的软件.靠着它,我们写论文 ...