Python encode() 方法(转)
转自:http://www.cnblogs.com/wushuaishuai/p/7686290.html
描述
encode() 方法以指定的编码格式编码字符串,默认编码为 'utf-8'。
对应的解码方法:bytes decode() 方法。
语法
encode() 方法语法:
1
S.encode([encoding='utf-8'][,errors='strict'])
参数
- encoding -- 可选参数,要使用的编码,默认编码为 'utf-8'。
- errors -- 可选参数,设置不同错误的处理方案。默认为 'strict',意为编码错误引起一个UnicodeError。 其他可能得值有 'ignore', 'replace', 'xmlcharrefreplace', 'backslashreplace' 以及通过 codecs.register_error() 注册的任何值。
返回值
该方法返回编码后的字符串,它是一个 bytes 对象。
例子:
#!/usr/bin/env python# -*- coding: utf-8 -*-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'))print("gbk 解码: ", s_gbk.decode('gbk'))
输出:
菜鸟教程
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 解码: 菜鸟教程
备注:
- str利用decode方法根据str的编码将其解码为unicode字符串类型
- str利用encode根据特定的编码将unicode字符串类型转换为特定的编码
Python encode() 方法(转)的更多相关文章
- Python encode() 方法
描述 encode() 方法以指定的编码格式编码字符串,默认编码为 'utf-8'. 对应的解码方法:bytes decode() 方法. 语法 encode() 方法语法: S.encode([e ...
- 字符编码和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() 返回值:返回大小写字母转换后生成的新字符串 ...
随机推荐
- DataGridView 绑定数据问题及修改值交换列
- docker测试时候命令无法补全的解决方法_docker
发现问题 在输入docker swarm 然后tab键不能像这样进行提示 和补全 tab 键也无法补全nginx容器名,下面是运行截图 解决方法: yum install -y bash-comple ...
- Android工程运用阿里freeline10秒快速编译分享
git地址:https://github.com/alibaba/freeline 目前已经更新到0.6.0版本. 原来编译一次需要几分钟甚至几十分钟的android工程,运用freeline,1分钟 ...
- Mars的简单使用
- 来自极客头条的 35 个 Java 代码性能优化总结
前言 代码优化,一个很重要的课题.可能有些人觉得没用,一些细小的地方有什么好修改的,改与不改对于代码的运行效率有什么影响呢?这个问题我是这么考虑的,就像大海里面的鲸鱼一样,它吃一条小虾米有用吗?没用, ...
- Reactor 3 学习笔记(2)
接上篇继续学习各种方法: 4.9.reduce/reduceWith @Test public void reduceTest() { Flux.range(1, 10).reduce((x, y) ...
- AngularJS中$interval的用法
在AngularJS中$interval用来处理间歇性处理一些事情. 最常用的是: var app = angular.module("app",[]); app.controll ...
- 关于Hook CreateMutex
我是个驱动新手,最近学习破解多开.经过一个通宵的百度和摸索,简单的多开kugou用以下代码可以了. MyNtCreateMutant( OUT PHANDLE MutantHandle, IN ACC ...
- [leetcode]Permutation Sequence @ Python
原题地址:https://oj.leetcode.com/submissions/detail/5341904/ 题意: The set [1,2,3,…,n] contains a total of ...
- Android GUI之View测量
在上篇文章(http://www.cnblogs.com/jerehedu/p/4607599.html#gui)中,根据源码探索了View的绘制过程,过程有三个主要步骤,分别为测量.布局.绘制.系统 ...