bytes和bytearray
python - Bytes和Bytearray
标签(空格分隔): python-数据结构
bytes、bytearray
- [x] python3 引入两个新的类型
- [x]
bytes
- 不可变字节序列
- [x]
bytearry
- 字节数组
- 可变
- [x]
- [x] 字符串与bytes
- 字符串是字符组成的
有序序列
,字符可以使用编码来理解 bytes
是字节组成的有序的
,不可变序列
bytearray
是字节组成的有序的
,可变序列
- 字符串是字符组成的
- [x] 编码和解码
- 字符串按照不同的字符集编码
encode
返回字节序列 bytesencode(encoding="utf-8", error="strict") --> bytes
- 字节序列按照不同的字符集解码
decode
返回字符串bytes.decode(encoding="utf-8", error="strict") --> str
bytearray.decode(encoding="utf-8", error="strict") --> str
- 字符串按照不同的字符集编码
- [x] bytes定义
bytes()
空 bytesbytes(int)
指定字节的bytes,被 0 填充bytes(iterable_of_in) --> bytes
[0, 255] 的int组成的可迭代对象bytes(string, encoding[, errors]) -> bytes
等价于string.encode()
bytes(bytes_or_buffer) -> immutable copy of bytes_or_buffer
从一个字节序列或者buffer复制除一个新的不可变的bytes对象- 使用 b前缀定义
- 只允许 基本的 ASCII 使用字符形式 b"abc9"
- 使用十六进制表示 b"\x41\x61"
[x] bytes 操作
和str类型类似,都是不可变类型,所以方法很多都一样,只不过bytes的方法,输入是bytes,输出也是 bytes
>>> b"abc".find(b"b") ... 1 >>> b"abcdef".replace(b"f", b"123") ... b'abcde123'
- [x] 类方法
bytes.fromhex(string) ---> bytes
- string 必须是 2 个字符的 16进制的形式, "6162 6a 6b",空格将被忽略
>>> bytes.fromhex("6162 6a 6b") ... b'abjk' >>> bytes.fromhex("6162 6a 6b").decode() ... 'abjk' >>> "中".encode() ... b'\xe4\xb8\xad' >>> bytes.fromhex("e4b8ad") ... b'\xe4\xb8\xad' >>> bytes.fromhex("e4b8ad").decode() ... '中' >>> "{:X}{:X}{:X}".format(*"中".encode()) ... 'E4B8AD' >>> bytes.fromhex("{:X}{:X}{:X}".format(*"中".encode())) ... b'\xe4\xb8\xad' >>> bytes.fromhex("{:X}{:X}{:X}".format(*"中".encode())).decode() ... '中'
[x]
hex() ---> int
返回 16 进制表示的字符串
>>> "abc".encode().hex() ... '616263' >>> "中".encode().hex() ... 'e4b8ad' >>> bytes.fromhex("中".encode().hex()) ... b'\xe4\xb8\xad' >>> bytes.fromhex("中".encode().hex()).decode() ... '中'
- [x] 类方法
- [x] bytearray 定义
bytearray()
空bytearray
bytearray(int)
指定字节的bytearray
,被 0 填充bytearray(iterable_of_in) --> bytearray
[0, 255] 的int组成的可迭代对象bytearray(string, encoding[, errors]) -> bytearray
近似于string.encode()
,不过返回可变的对象bytearray(bytes_or_buffer) -> immutable copy of bytes_or_buffer
从一个字节序列或者buffer复制除一个新的不可变的bytearray对象
注意: b 前缀定义的类型是 bytes类型的
[x] bytearray 操作
和 bytes 类型的方法相同
>>> bytearray(b"abc").find(b"b") ... 1 >>> bytearray(b"abcdef").replace(b"f", b"123") ... bytearray(b'abcde123')
- [x] 类方法
bytearray.fromhex(string) ---> bytearray
- string 必须是 2 个字符的 16进制的形式, "6162 6a 6b",空格将被忽略
>>> bytearray.fromhex("6162 6a 6b") ... bytearray(b'abjk') >>> bytearray.fromhex("6162 6a 6b").decode() ... 'abjk' >>> "中".encode() ... b'\xe4\xb8\xad' >>> bytearray.fromhex("e4b8ad") ... bytearray(b'\xe4\xb8\xad') >>> bytearray.fromhex("e4b8ad").decode() ... '中' >>> "{:X}{:X}{:X}".format(*"中".encode()) ... 'E4B8AD' >>> bytearray.fromhex("{:X}{:X}{:X}".format(*"中".encode())) ... bytearray(b'\xe4\xb8\xad') >>> bytearray.fromhex("{:X}{:X}{:X}".format(*"中".encode())).decode() ... '中'
[x]
hex() ---> int
返回 16 进制表示的字符串
[x]
索引 ---> int
bytearray(b"abcdef")[2]
返回改字节对应的数,int 类型>>> bytearray(b"abcdef")[2] ... 99
- [x]
append(int)
尾部追加一个元素 - [x]
insert(index,int)
在指定索引位置上插入元素 - [x]
extend(iterable_int)
将一个可迭代的整数集合追加到当前的bytearry
- [x]
pop(index=-1)
从指定索引上移除元素,默认从尾部移除 - [x]
remove(value)
找到一个value
移除,找不到 则ValueError
异常
上述方法中若要使用int类型,值必须在 [0, 255] 之间
- [x]
clear()
清空bytearry
- [x]
reverse()
翻转bytearry
,就地修改
- [x] 类方法
转载于:https://www.cnblogs.com/jingru-QAQ/p/11405626.html
bytes和bytearray相关推荐
- python bytearray拼接_python数据类型 ——bytes 和 bytearray
bytes和 bytearray bytes:可以看作是一组二进制数值(0-255) 的 str 序列 bytearray:可以看作是一组二进制数值(0-255) 的 list 序列 bytes类型 ...
- TypeError: the JSON object must be str, bytes or bytearray, not dict
TypeError: the JSON object must be str, bytes or bytearray, not dict 目录 TypeError: the JSON object m ...
- python3 bytes和bytearray总结
The core built-in types for manipulating binary data are bytes and bytearray. They are supported by ...
- Python3: 字节类型 bytes 和 bytearray
本文链接: https://blog.csdn.net/xietansheng/article/details/115557553 Python3 学习笔记(目录) 1. bytes Python 官 ...
- python中byte2array报错_python基础-bytes和bytearray的用法
Python中的序列类型有bytes和bytearray. 二进制序列类型的用法比较少见,是python中少用的一种序列类型,对于二进制序列类型,大家基本了解即可. bytes二进制序列类型 指定长度 ...
- python中byte类型_详解python string类型 bytes类型 bytearray类型
搜索热词 一.python3对文本和二进制数据做了区分.文本是Unicode编码,str类型,用于显示.二进制类型是bytes类型,用于存储和传输.bytes是byte的序列,而str是unicode ...
- Python 数据类型 bytes 与 bytearray 使用教程
bytes 与 bytearray 是python非常重要的数据类型,但其重要性经常被我们忽视了.在实际开发过程中,又总是遇到 bytes 类型.举例,pickle 序列化, json序列化就是将对象 ...
- Python: bytes、bytearray 详解
文章目录 写在开头的话 Python: bytes.bytearray 00. 概述 01.bytes定义 02. bytes操作 03. bytearray定义 04. bytearray操作 05 ...
- Python:bytes、bytearray
bytes.bytearray Python3引入两个新类型 bytes:不可变字节序列 bytearray:字节数组.可变 字符串与bytes 字符串是字符组成的有序序列,字符可以使用编码来理解 b ...
最新文章
- 盘点:弱电施工过程中的电线电缆36计
- Python 5种方法实现单例模式
- 【数据迁移】使用传输表空间迁移数据
- boost::hana::symmetric_difference用法的测试程序
- ITK:将2D图像堆叠为3D图像
- FireBug命令行命令介绍
- 4.28—013—周日
- 计算机网络读书笔记(1)
- 游戏日志分析2:全方位数据采集
- linux socket句柄泄露,socket句柄泄漏问题的定位: losf和strace的联合使用!
- CentOs 开启ssh服务
- 酷似js的java函数简写——lambda表达式
- Excel批量打开URL
- Detours内联HOOK
- 微信小程序vtabs
- javaweb项目大概轮廓
- IDEA 在debug 模式下启动tomcat报错:Application Server was not ..reason:Unable to ping server at localhos:1199
- Hides for Mac(隐藏程序Mac老板键)
- 牛客网之黑暗的字符串
- Vue - 路由导航守卫控制访问权限,设置 localStorage 过期时间