python中encode()函数的用法
python字符串函数用法大全链接
encode()函数
描述:以指定的编码格式编码字符串,默认编码为 'utf-8'。
语法:str.encode(encoding='utf-8', errors='strict') -> bytes (获得bytes类型对象)
- encoding 参数可选,即要使用的编码,默认编码为 'utf-8'。字符串编码常用类型有:utf-8,gb2312,cp936,gbk等。
- errors 参数可选,设置不同错误的处理方案。默认为 'strict',意为编码错误引起一个UnicodeEncodeError。 其它可能值有 'ignore', 'replace', 'xmlcharrefreplace'以及通过 codecs.register_error() 注册其它的值。
程序示例:
str1 = "我爱祖国"
str2 = "I love my country"
print("utf8编码:",str1.encode(encoding="utf8",errors="strict")) #等价于print("utf8编码:",str1.encode("utf8"))
print("utf8编码:",str2.encode(encoding="utf8",errors="strict"))
print("gb2312编码:",str1.encode(encoding="gb2312",errors="strict"))#以gb2312编码格式对str1进行编码,获得bytes类型对象的str
print("gb2312编码:",str2.encode(encoding="gb2312",errors="strict"))
print("cp936编码:",str1.encode(encoding="cp936",errors="strict"))
print("cp936编码:",str2.encode(encoding="cp936",errors="strict"))
print("gbk编码:",str1.encode(encoding="gbk",errors="strict"))
print("gbk编码:",str2.encode(encoding="gbk",errors="strict"))
程序运行结果:
utf8编码: b'\xe6\x88\x91\xe7\x88\xb1\xe7\xa5\x96\xe5\x9b\xbd'
utf8编码: b'I love my country'
gb2312编码: b'\xce\xd2\xb0\xae\xd7\xe6\xb9\xfa'
gb2312编码: b'I love my country'
cp936编码: b'\xce\xd2\xb0\xae\xd7\xe6\xb9\xfa'
cp936编码: b'I love my country'
gbk编码: b'\xce\xd2\xb0\xae\xd7\xe6\xb9\xfa'
gbk编码: b'I love my country'
注:在python中encode和decode分别指编码和解码
python中encode()函数的用法相关推荐
- python中int函数的用法浅析_Python中int()函数的用法浅析
int()是Python的一个内部函数 Python系统帮助里面是这么说的 >>> help(int) Help on class int in module __builtin__ ...
- python中int用法,Python中int()函数的用法浅析
int()是Python的一个内部函数 Python系统帮助里面是这么说的 >>> help(int) Help on class int in module __builtin__ ...
- python中pop函数的用法_python中pop()函数怎么用
python中pop()函数的用法:pop()函数用于移除列表中的一个元素(默认最后一个元素),并且返回从列表中移除的元素对象.函数语法:[list.pop(ojb=list[-1])]. pop() ...
- python中get函数是什么意思_详解python中get函数的用法(附代码)_后端开发
strncmp函数用法详解_后端开发 strncmp函数为字符串比较函数,其函数语法为"int strncmp ( const char * str1, const char * str2, ...
- python中count函数的用法
Python count()方法 描述 Python count() 方法用于统计字符串里某个字符出现的次数.可选参数为在字符串搜索的开始与结束位置. count()方法语法: str.count(s ...
- Python中Print()函数的用法___实例详解(二)(全,例多)
Python中Print()函数的用法___实例详解(二)(全,例多) 目录 十一.Print()小例子 十二.Print()中文输入显示乱码问题 十三.Print()写入文件 十四.print()在 ...
- python中divmod函数的用法_Python中divmod函数的用法
Python中divmod函数的用法,语言,余数,是一种,面向对象,函数 Python中divmod函数的用法 Python中divmod函数的用法 在Python中divmod函数的作用是把除数和余 ...
- python中sorted函数的用法_Python3 中sorted() 函数的用法
描述 sorted() 函数对所有可迭代的对象进行排序操作. 语法 sorted(iterable, key=None, reverse=False) iterable – 可迭代对象. key – ...
- python中zip()函数的用法_Python zip()函数用法实例分析
本文实例讲述了python zip()函数用法.分享给大家供大家参考,具体如下: 这里介绍python中zip()函数的使用: >>> help(zip) Help on built ...
最新文章
- 马鞍山职业计算机考试,2020年职业适应性(技能)测试纲要
- 【企业管理】企业创造的要素
- java不会框架怎么办_感觉学java无从下手了,各种框架乱七八糟,感觉好乱。该怎么办!?...
- php计算对角线,python计算对角线有理函数插值的方法
- java double 值是6.346255785955615E-4,这是字母“E”什么意思
- ADO.NET实例教学一
- ppt设置外观样式_ppt设置图片样式_PPT如何快速修改图像样式图文教
- c盘瘦身 或者 c盘清理
- vue 项目完美运行在IE或者360浏览器兼容模式下 踩坑笔记
- shiro学习详解(开篇)
- 【Pandas数据处理100例目录】Python数据分析玩转Excel表格数据
- 《WEB性能测试实战》之WEB性能测试分析
- 严重: 所必需的服务组件启动失败,所以无法启动Tomcat,eclipse:org.apache.catalina.startup.Catalina start
- asp.net医院信息管理系统VS开发sqlserver数据库web结构c#编程计算机网页源码项目
- python123查找指定字符输入m_python基础--2 字符串
- 在Matlab中提取彩图黄色分量
- 装X利器:做一个Python爬虫小工具——图片下载器
- 杜教筛 狄利克雷卷积入门
- 智慧矿山 | 基于钻孔数据的三维地质模型可视化
- Arduino 下用A4988或TMC2209驱动42步进电机
热门文章
- OpenStack常见命令与问题集合
- E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?
- 一文读懂架构师都不知道的isinstance检查机制
- 很久很久之前的一道面试题(老师的生日是那一天?)~
- 【PostgreSQL-9.6.3】事件触发器
- JAVA分布式架构的演变及解决方案
- [Git]解决Permission denied, please try again问题
- 解决spring-boot-maven-plugin插件打包,springboot启动时报找不到主main问题
- .NET Core,.NET Framework和Xamarin有什么区别?
- 我可以编写一个CSS选择器来选择不具有某个类或属性的元素吗?