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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • kafka topicPartitions问题

    kafka topicPartitions问题当我实现三个消费者去同时消费一个Topic(默认没有分区)消息时,三个消费者同时消费到了同样的消息现象如下:2019-06-1115:30:02.516[org.springframework.kafka.KafkaListenerEndpointContainer#0-0-C-1]INFOcom.example.kafka.consumer.TestB-==[cousumerC…

    2022年6月26日
    23
  • SP网站建立后.有两处地方需要首先修改

    SP网站建立后.有两处地方需要首先修改1.SP管理中心—-系统设置—-配置备用访问映射内部URL区域区域的公用URL http://kms:504 默认http://kms:504 http://localhost:504 Intranethttp://localhost:504 http://192.168.1.109:504 Internethttp://192.168.1.109:5

    2022年6月17日
    31
  • MySQL数据类型DECIMAL用法

    MySQL数据类型DECIMAL用法MySQL DECIMAL数据类型用于在数据库中存储精确的数值。我们经常将DECIMAL数据类型用于保留准确精确度的列,例如会计系统中的货币数据。要定义数据类型为DECIMAL的列,请使用

    2022年7月1日
    23
  • 最流行的三大数据建模工具

    最流行的三大数据建模工具越来越多的业务,越来越多的信息化系统,让很多公司拥有了海量数据,但是分散的数据、隔离的系统,又形成了一个个数据孤岛。于是,为了利用好数据,各大公司纷纷建设了数据仓库,或者是最近升级为大数据平台之类的,但是,不同条线不同场景的数据又要如何整合到同一个仓库呢?数据模型就此应运而生,通过高度抽象的数据模型,整合各个源系统的数据,最终形成统一、规范、易用的数据仓库,进而提供包括数据集市、数据挖掘…

    2022年6月1日
    39
  • shiro框架—shiro配置介绍(一)

    shiro框架—shiro配置介绍(一)接上一篇文章shiro框架—关于用户登录和权限验证功能的实现步骤(二)shiro在springboot项目中的配置步骤1、引入依赖  首先shiro的应用,引入的依赖仅仅只有一个,即下边这个。&amp;amp;amp;amp;lt;dependency&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;groupId&amp;amp;amp;amp;gt;org.apache.shiro&amp;amp

    2025年10月3日
    2
  • 《MySQL45讲》读书笔记(六):数据库事务概述[通俗易懂]

    《MySQL45讲》读书笔记(六):数据库事务概述[通俗易懂]此文为极客时间:MySQL实战45讲的3、8、18、19节事务相关部分的总结一、事务的启动方式mysql主要有两种事务的启动方式:begin或starttransaction显式启动事

    2022年8月16日
    5

发表回复

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

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