bytes和bytearray总结
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总结的更多相关文章
- 关于bytes和bytearray
背景 平时工作因为有批量线上数据进行更新,通过Python程序连接数据库,利用连接池和gevent的并发性能,处理大量数据. 因为数据方提供的数据表结构中带有varbinary类型字段,并非全部,所以 ...
- python bytes和bytearray、编码和解码
str.bytes和bytearray简介 str是字符数据,bytes和bytearray是字节数据.它们都是序列,可以进行迭代遍历.str和bytes是不可变序列,bytearray是可变序列,可 ...
- 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 ...
- Python的程序结构[2] -> 类/Class[5] -> 内建类 bytes 和 bytearray
内建类 bytes 和 bytearray / Built-in Type bytes and bytearray 关于内建类 Python的内建类 bytes 主要有以下几点: class byte ...
- python string类型 bytes类型 bytearray类型
一.python3对文本和二进制数据做了区分.文本是Unicode编码,str类型,用于显示.二进制类型是bytes类型,用于存储和传输.bytes是byte的序列,而str是unicode的序列. ...
- bytes和bytearray
bytes bytes是Python 3中特有的,Python 2 里不区分bytes和str. Python 2中 >>> type(b'xxxxx') <type 'str ...
- Python字节数组【bytes/bytearray】
bytes >>> type(b'xxxxx') <class 'bytes'> >>> type('xxxxx') <class 'str'&g ...
- bytes,bytearray
1.bytes.bytearray ---Python3 引入的! bytes:不可变字节序列,bytearray:字节属组,可变 都是连续的空间. 2.字符串与bytes 字符串是字符组成的有序的序 ...
- 【Python】【内置函数】【bytes&bytearray&str&array】
[bytes] 英文文档: class bytes([source[, encoding[, errors]]]) Return a new “bytes” object, which is an i ...
随机推荐
- Diameter of Binary Tree
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a b ...
- Docker 运行的 应用程序无法连接Oracle数据库的解决办法
1. 最近公司使用docker化部署运行 app 发现一个部门的 多数据源取数的功能连接不上 oracle数据库 报错提示为: 2. 公司平台部同事给出两个解决方案: https://blog.cs ...
- 医院医疗类报表免费用,提反馈,还能赢取P30!
医院医疗类报表免费用,提反馈,还能赢取P30! “葡萄城报表模板库是一款免费的报表制作.学习和参考工具,包含了超过 200 张高质量报表模板,涵盖了 16 大行业和 50 多种报表类型,为 30 余万 ...
- 坦克大战--Java类型 ---- (2)按键设置和用户名的输入
一.实现思路(emmmm,这个地方我很大程度参照了别人的写法) 由于键盘按键众多,因此使用选择框JComboBox 进行按键选择,点击一个JButton 按钮后,读取所有选择框中的内容,然后存到一 ...
- 一些通用的js工具类,添加自定义插件
common_t.js /** * 通用工具组件 对原有的工具进行封装,自定义某方法统一处理<br> * ^_^ * * Author: em.D * Date: 2016-05-17 * ...
- Echarts饼图将数据显示在 legend 旁边
不多废话,笔记如下 var myEcharts = echarts.init(document.getElementById('doughnut')); option = { tooltip: { t ...
- sql server if else
DECLARE IF (@sex = '1')BEGIN PRINT '2'END ELSE BEGIN PRINT(1) END begin... end可以省略 declare @sex int ...
- golang(4):函数 & 数组 & 切片 & map & 锁
内置函数 // 1. close:主要用来关闭channel // 2. len:用来求长度,比如string.array.slice.map.channel // 3. new:用来分配内存,主要用 ...
- ccs之经典布局(三)(等分,等高布局)
接上篇ccs之经典布局(二)(两栏,三栏布局) 七.等分布局 等分布局是指一行被分为若干列,每一列的宽度是相同的值.两列之间有若干的距离. 1.float+padding+background-cli ...
- WebSocket的兼容性
https://github.com/sockjs/sockjs-client https://socket.io/ https://github.com/gimite/web-socket-js h ...