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. Eclipse myeclipse下配置HanLP的教程

    一.说明 博主的配置 1:window10 2:myeclipse 3:jdk1.8 备注:文章分享自贾继康的博客,博客使用的hanlp是1.6.8的版本.大家可以去下载最新的1.7版本了,也比较推荐 ...

  2. Configure脚本支持说明

    在Linux上安装Nginx需要执行Configure脚本,该脚本需要做一些参数说明: 选项 说明 --prefix=<path> 指定Nginx软件的安装路径,若不指定默认安装在/usr ...

  3. 从入门到自闭之Python递归

    递归:不断地调用自身,用函数实现 死递归(死循环): def func(): print(1) func() func() 知识点:官方说明最大深度1000,但实际执行998或997以下,看电脑性能 ...

  4. 如何用纯 CSS 创作一个精彩的彩虹 loading 特效

    效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/vjvoow 可交互视频教 ...

  5. python-socketserver实例

    import socketserver class MyTCPHandler(socketserver.BaseRequestHandler): def handle(self): while Tru ...

  6. GoAccess操作手册

    名字 GoAccess - 可视化 Web 日志分析工具. 语法 goaccess [filename] [ options ... ] [-c][-M][-H][-q][-d][...] 描述 Go ...

  7. redis 学习(9)-- redis 客户端 -- redis-py

    redis 客户端 -- redis-py 简介 关于 redis 的各种客户端,我们可以在官网上寻找并使用,比如我这里的 python 客户端,可以在官网上找到:redis-client . 获取 ...

  8. pc端和移动端的“窗口”(viewport)故事(part1)

    A tale of two viewports - part one 在以下的系列文章中,我将为大家解释浏览器中的视窗和一些重要的元素的尺寸是如何起作用的,如:大家最熟悉的html元素以及window ...

  9. ArrayList扩容分析

    一段java代码 String e = "q3234v"; List<String> list = new ArrayList<String>(); for ...

  10. python发起post请求获取json数据使用requests方法

    最普通的答案 我一直就觉得GET和POST没有什么除了语义之外的区别,自打我开始学习Web编程开始就是这么理解的 . 可能很多人都已经猜到了答案是: 1.GET 使用URL或Cookie传参.而POS ...