python中encode和decode的区别_python rindex

python中encode和decode的区别_python rindex1.基本语法1.encode()和decode()都是字符串的函数decode解码encode编码str———>str(Unicode,byte类型)———>str2.decode()与encode()方法可以接受参数,其声明分别为:其中的encoding是指在解码编码过程中使用的编码(此…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

 1.基本语法

1.encode()和decode()都是字符串的函数

      decode解码             encode编码
str ---------> str(Unicode,byte类型) ---------> str

  2.decode()与encode()方法可以接受参数,其声明分别为:

 其中的encoding是指在解码编码过程中使用的编码(此处指“编码方案”是名词),errors是指错误的处理方案。

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

3.查看官网关于encode与decode方法的使用说明如下:

1.str.encode(encoding=”utf-8″errors=”strict”)

Return an encoded version of the string as a bytes object. Default encoding is 'utf-8'errors may be given to set a different error handling scheme. The default for errors is 'strict', meaning that encoding errors raise a UnicodeError. Other possible values are 'ignore''replace''xmlcharrefreplace','backslashreplace' and any other name registered via codecs.register_error(), see section Error Handlers. For a list of possible encodings, see section Standard Encodings.

2.bytes.decode(encoding=”utf-8″errors=”strict”)

Return a string decoded from the given bytes. Default encoding is 'utf-8'errors may be given to set a different error handling scheme. The default for errors is 'strict', meaning that encoding errors raise a UnicodeError. Other possible values are 'ignore''replace' and any other name registered viacodecs.register_error(), see section Error Handlers. For a list of possible encodings, see section Standard Encodings.

2.使用演示与注意事项


a = '编码测试'

#使用不同的编码格式给a进行编码
b = a.encode('utf-8')
c = a.encode('gb2312') #发现gb2312和gbk结果一样
d = a.encode('gbk')
print(type(b),b)
print(type(c),c)
print(type(d),d)
'''
<class 'bytes'> b'\xe7\xbc\x96\xe7\xa0\x81\xe6\xb5\x8b\xe8\xaf\x95'
<class 'bytes'> b'\xb1\xe0\xc2\xeb\xb2\xe2\xca\xd4'
<class 'bytes'> b'\xb1\xe0\xc2\xeb\xb2\xe2\xca\xd4'
'''
#使用不同的解码方式解码
b1 = b.decode('utf-8')
c1 = c.decode('gb2312')
d1 = d.decode("gbk")
b11 = b.decode('gbk')  #b本来是用utf-8编码,现在用gbk进行解码,出现乱码的情况
print(type(b1),b1)
print(type(c1),c1)
print(type(d1),d1)
print(type(b11),b11)  #b本来是用utf-8编码,现在用gbk进行解码,出现乱码的情况
'''
<class 'str'> 编码测试
<class 'str'> 编码测试
<class 'str'> 编码测试
<class 'str'> 缂栫爜娴嬭瘯
'''
  • 字符串通过编码成为字节码,字节码通过解码成为字符串。 
  • 字符串或者字节只能同时拥有一个方法 ,要么解码要么编码

统一声明:关于原创博客内容,可能会有部分内容参考自互联网,如有原创链接会声明引用;如找不到原创链接,在此声明如有侵权请联系删除哈。关于转载博客,如有原创链接会声明;如找不到原创链接,在此声明如有侵权请联系删除哈。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/185267.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • React 时间控件

    React 时间控件使用bootstrap的datetimepicker安装:npminstallreact-bootstrap-datetimepicker使用bootstrap的datetime安装:npminstall–savereact-datetime若使用中文,还需要安装moment,这样datetime插件中的locale属性(控制语言)才能够正常使用:安装moment:npminstal

    2022年5月23日
    81
  • 伪代码的写法[通俗易懂]

    伪代码的写法[通俗易懂]伪代码(Pseudocode)是一种算法描述语言。使用伪代码的目的是为了使被描述的算法可以容易地以任何一种编程语言(Pascal,C,Java,etc)实现。因此,伪代码必须结构清晰、代码简单、可读性好,并且类似自然语言。介于自然语言与编程语言之间。  它以编程语言的书写形式指明算法的职能。相比于程序语言(例如Java,C++,C,Dephi等等)它更类似自然语言。它是半角式化、不标准的语言

    2022年6月3日
    130
  • Centos7下Rinetd安装与应用[通俗易懂]

    转自http://www.cnblogs.com/zhenyuyaodidiao/p/5540209.htmlLinux下做地址NAT有很多种方法。比如haproxy、nginx的4层代理,linux自带的iptables等都能实现。haproxy、nginx就不说了,配置相对简单;iptables配置复杂,概念也比较多DNAT、SNAT、PREROUTING、POSTROUTING等等。其实

    2022年4月17日
    80
  • signed apk error [WifiManagerLeak]

    signed apk error [WifiManagerLeak]

    2021年9月30日
    44
  • httpClient4 设置代理「建议收藏」

    httpClient4 设置代理「建议收藏」HttpHostproxy=newHttpHost(p.getIp(),p.getPort());  httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy);

    2022年7月22日
    12
  • Page.RegisterStartupScript 的用法

    Page.RegisterStartupScript 的用法轉自:http://www.cnblogs.com/jxyxhz/archive/2008/11/25/1340432.htmlPage_load调用JS函数以及confirm的用法protectedvoidPage_Load(objectsender,EventArgse)   {       if(!IsPostBack)       {    

    2022年7月20日
    13

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号