
本文共 3180 字,大约阅读时间需要 10 分钟。
bytes、bytearray
Python3引入两个新类型
- bytes:不可变字节序列
- bytearray:字节数组、可变
字符串与bytes
字符串是字符组成的有序序列,字符可以使用编码来理解
bytes是字节组成的有序的不可变序列
bytearray是字节组成的有序的可变序列
编码与解码
字符串按照不同的字符集编码encode返回字节序列bytes
encode(encoding='utf-8', errors='strict') -> bytes
字节序列按照不同的字符集解码decode返回字符串
bytes.decode(encoding="utf-8", errors="strict") -> str
bytearray.decode(encoding="utf-8", errors="strict") -> str
bytes定义
bytes() 空bytes
a = bytes()print(type(a)) #bytes(int) 指定字节的bytes,被0填充
a = bytes(4)print(a) #b'\x00\x00\x00\x00'bytes(iterable_of_ints) -> bytes [0,255]的int组成的可迭代对象
a = bytes(range(3))print(a) #b'\x00\x01\x02'bytes(string, encoding[, errors]) -> bytes 等价于string.encode()
print(bytes("abc","utf8")) #b'abc'print("abc".encode("utf8")) #b'abc'bytes(bytes_or_buffer) -> immutable copy of bytes_or_buffer 从一个字节序列或者buffer复制出 一个新的【相当于前copy】
a = "abc".encode("utf8")b = bytes(a)print(id(a) == id(b)) #True
bytes操作【注意:下述方法若需要使用int类型,值在[0, 255]】
和str类型类似,都是不可变类型,所以方法很多都一样。只不过bytes的方法,输入是bytes,输出是 bytes
a.split() #切割
a = bytes("abc","utf8")print(a.split(b"b")) #[b'a', b'c']类方法 bytes.fromhex(string)
- string必须是2个字符的16进制的形式,'6162 6a 6b',空格将被忽略
print(bytes.fromhex('6162 09 6a 6b00')) #b'ab\tjk\x00'返回16进制表示的字符串
print("abc".encode("utf8").hex()) #616263索引
print("abc".encode("utf8")[1]) #98
bytearray定义
bytearray
bytearray() 空bytearray
a = bytearray()print(type(a)) #bytearray(int) 指定字节的bytearray,被0填充
a = bytearray(5)print(a) #bytearray(b'\x00\x00\x00\x00\x00')bytearray(iterable_of_ints) -> bytearray [0,255]的int组成的可迭代对象
a = bytearray(range(3))print(a) #bytearray(b'\x00\x01\x02')bytearray(string, encoding[, errors]) -> bytearray 近似string.encode(),不过返回可变对象
a = bytearray("abc","utf8")print(a) #bytearray(b'abc')bytearray(bytes_or_buffer) 从一个字节序列或者buffer复制出一个新的可变的bytearray对象
a = bytearray("abc","utf8")b = bytearray(a)print(b) #bytearray(b'abc')
bytearray操作【注意:下述方法若需要使用int类型,值在[0, 255]】
和bytes类型的方法相同
print(bytearray(b'abcdef').replace(b'f',b'k')) #bytearray(b'abcdek')bytearray(b'abc').find(b'b') #1类方法 bytearray.fromhex(string)
- string必须是2个字符的16进制的形式,'6162 6a 6b',空格将被忽略
print(bytearray.fromhex('6162 09 6a 6b00')) #bytearray(b'ab\tjk\x00')hex() #返回16进制表示的字符串
print(bytearray('abc'.encode()).hex()) #616263索引
print(bytearray(b'abcdef')[2]) #99append(int) 尾部追加一个元素
b = bytearray()b.append(97) print(b) #bytearray(b'a')insert(index, int) 在指定索引位置插入元素
b = bytearray()b.append(97)b.insert(1,98)print(b) #bytearray(b'ab')extend(iterable_of_ints) 将一个可迭代的整数集合追加到 当前bytearray
b = bytearray()b.append(97)b.insert(1,98)b.extend([65,66,67])print(b) #bytearray(b'ab') #bytearray(b'abABC')pop(index=-1) 从指定索引上移除元素,默认从尾部移除
b = bytearray()b.append(97)b.insert(1,98)b.extend([65,66,67])b.pop(-1)print(b) #bytearray(b'abAB')remove(value) 找到第一个value移除,找不到抛 ValueError异常
b = bytearray()b.append(97)b.insert(1,98)b.extend([65,66,67])b.pop(-1)b.remove(66)print(b) #bytearray(b'abA')clear() 清空bytearray
b = bytearray()b.append(97)b.insert(1,98)b.extend([65,66,67])b.pop(-1)b.remove(66)b.reverse()b.clear()print(b) #bytearray(b'')reverse() 翻转bytearray,就地修改
b = bytearray()b.append(97)b.insert(1,98)b.extend([65,66,67])b.pop(-1)b.remove(66)b.reverse()print(b) #bytearray(b'Aba')
发表评论
最新留言
关于作者
