The core built-in types for manipulating binary data are bytes and bytearray. They are supported by memoryview which uses the buffer protocol to access the memory of other binary objects without needing to make a copy.

bytearray objects are a mutable counterpart to bytes objects.

bytes定义

1.使用bytes函数创建bytes

  • bytes() 创建一个空的bytes
  • bytes(int) 创建一个int位的全位0的bytes
  • bytes(iterabl_of_ints) 可迭代数字组成的bytes(比如range)
  • bytes(string,encoding[,errors]) 等价于string.encode()
  • bytes(bytes of buffer) 创建一个bytes的copy

2.直接定义

比如:

​ b = b'abc'

​ b = b'\x61'

3.类型转换

  • string.encode()
  • int.tobytes()
  • bytes.from

bytes函数定义

>>> bytes()
b''
>>> bytes(3)
b'\x00\x00\x00'
>>> bytes(range(3))
b'\x00\x01\x02'
>>> b = bytes('中国', encoding='utf-8')
>>> b
b'\xe4\xb8\xad\xe5\x9b\xbd'
>>> bytes(b)
b'\xe4\xb8\xad\xe5\x9b\xbd'

直接创建:

>>> b = b'abc'
>>> b
b'abc'

类型转换:

>>> n = 97
>>> n.to_bytes(1,byteorder='big')
b'a'
>>> s = '中国'

>>> s.encode(encoding = 'utf-8')
b'\xe4\xb8\xad\xe5\x9b\xbd'

>>> bytes.fromhex('61')
b'a'

bytes的显示方式

Only ASCII characters are permitted in bytes literals (regardless of the declared source code encoding). Any binary values over 127 must be entered into bytes literals using the appropriate escape sequence.

只有ASCII中的字符串是可以直接在bytes类型中显示出来的,所有大于127的数值用转义字符表达。

比如,内存中的字节对象用十六进制表示为61,在python中显示的方式不是b'\x61' 而是b'a';而b'\xe4'显示方式就是b'\xe4';注意:仅仅是显示方式而已

另外,并不是所有的小于127的都可以被友好的显示出来,有些对象本身不可显示,就显示其十六进制表示。比如

b'\x00'

bytes的一般方法

bytes类似于string;在方法上,除了自己特有的方法外,跟str也类似。

比如:

>>> b'abc'.find(b'\x63')
2
>>> b'abc'.replace(b'\61',b'A')
b'abc'

bytearray定义

bytearray是可变的bytes数据类型,可以通过bytearray创建和定义

一:bytearray()定义

  • bytearray() 创建一个空的bytearray
  • bytearray(int) 创建一个int位的全位0的bytearray
  • bytearray(iterabl_of_ints) 可迭代数字组成的bytearray(比如range)
  • bytearray(string,encoding[,errors]) 将一个字符串编码为bytearray
  • bytearray(bytes of buffer) 创建一个bytearray

二: bytearray的方法定义

  • bytearray.fromhex()

bytearray的一般方法

bytearray具备bytes的操作方法,像字符串一样操作;

另外bytearray还具备像list一样的操作方法,比如pop,append等

bytes 和 bytearray的方法

十六进制和字节类型的相互转换

  • bytes.fromhex()
  • bytearray.fromhex()
  • bytes.hex()
  • bytesarray.hex()
>>> b = bytes('hell',encoding='utf-8')
>>> ba = bytearray('hell',encoding='utf-8')
>>> b
b'hell'
>>> ba
bytearray(b'hell')
>>> b.hex()
'68656c6c'
>>> ba.hex()
'68656c6c'

bytes和bytearray总结的更多相关文章

  1. 关于bytes和bytearray

    背景 平时工作因为有批量线上数据进行更新,通过Python程序连接数据库,利用连接池和gevent的并发性能,处理大量数据. 因为数据方提供的数据表结构中带有varbinary类型字段,并非全部,所以 ...

  2. python bytes和bytearray、编码和解码

    str.bytes和bytearray简介 str是字符数据,bytes和bytearray是字节数据.它们都是序列,可以进行迭代遍历.str和bytes是不可变序列,bytearray是可变序列,可 ...

  3. python中读取json文件报错,TypeError:the Json object must be str, bytes or bytearray,not ‘TextIOWrapper’

    利用python中的json读取json文件时,因为错误使用了相应的方法导致报错:TypeError:the Json object must be str, bytes or bytearray,n ...

  4. Python的程序结构[2] -> 类/Class[5] -> 内建类 bytes 和 bytearray

    内建类 bytes 和 bytearray / Built-in Type bytes and bytearray 关于内建类 Python的内建类 bytes 主要有以下几点: class byte ...

  5. python string类型 bytes类型 bytearray类型

    一.python3对文本和二进制数据做了区分.文本是Unicode编码,str类型,用于显示.二进制类型是bytes类型,用于存储和传输.bytes是byte的序列,而str是unicode的序列. ...

  6. bytes和bytearray

    bytes bytes是Python 3中特有的,Python 2 里不区分bytes和str. Python 2中 >>> type(b'xxxxx') <type 'str ...

  7. Python字节数组【bytes/bytearray】

    bytes >>> type(b'xxxxx') <class 'bytes'> >>> type('xxxxx') <class 'str'&g ...

  8. bytes,bytearray

    1.bytes.bytearray ---Python3 引入的! bytes:不可变字节序列,bytearray:字节属组,可变 都是连续的空间. 2.字符串与bytes 字符串是字符组成的有序的序 ...

  9. 【Python】【内置函数】【bytes&bytearray&str&array】

    [bytes] 英文文档: class bytes([source[, encoding[, errors]]]) Return a new “bytes” object, which is an i ...

随机推荐

  1. (5.1)mysql高可用系列——高可用架构方案概述

    关键词:mysql高可用概述,mysql高可用架构 常用高可用方案 20190918 现在业内常用的MySQL高可用方案有哪些?目前来说,用的比较多的开源方案分内置高可用与外部实现,内置高可用有如下: ...

  2. 小记---------破解idea2018.3.6 转载

      一.进入idea官网选择想要下载的版本 官网版本选择页面: https://www.jetbrains.com/idea/download/other.html IntelliJ IDEA 分为两 ...

  3. 使用Python基于OpenCV的验证码识别

    Blog:https://blog.csdn.net/qq_40962368/article/details/89312429(Verification_Code_Identification) 步骤 ...

  4. Java小程序—录屏小程序(上半场)

    做软件的三个步骤: (1)做什么? (2)怎么做? (3)动手做! ok,我们今天要做的是一个录屏软件,那怎么做呢?首先,我们小时候都玩过一种小人书,就是当你快速翻动书页时,书中的人物就会活灵活现的动 ...

  5. leecode刷题(26)-- 用栈实现队列

    leecode刷题(26)-- 用栈实现队列 用栈实现队列 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从队列首部移除元素. peek() -- 返 ...

  6. Java 获取日期间的日期 & 根据日期获取星期

    场景:根据起止日期获取中间的日期: 根据日期获取当前日期的星期 根据日期日期获取日期 /** * 获取日期间日期 * @param start * @param end * @return */ pr ...

  7. ueditor 后端配置项没有正常加载,上传插件不能正常使用 UTF8 PHP

    修改config.json 文件,用DW软件打开,修改好后,保存 若用记事本打开的话修改后保存,无法加载

  8. MySQL select之后再update

    1.先查询页面 而后再根据查询的结果来更改数据库,可以使用SELECT …… FOR UPDATE 来实现,具体的代码如下 SELECT * FROM chat //查询的表 //查询的条件 FOR ...

  9. Delphi ListBox组件

  10. 在centos7上kvm网卡桥接

    系统环境准备 [root@linux-node1 ~]# cat /etc/redhat-release CentOS Linux release (Core) [root@linux-node1 ~ ...