python-bytes-bytearray

bytes、bytearry

bytes(不可变字节序列)

bytearray(可变字节属组)

bytes初始化

#空bytes
print(bytes())    #输出:b''

#指定字节的bytes
print(bytes(2))   #输出:b'\x00\x00'

#bytes(iterable_of_ints)->bytes
print(bytes([0,255]))   #输出b'\x00\xff'

#bytes(string,encoding[,errors])->bytes等价于string.encode()
print(bytes('abc',encoding='utf8')) #输出b'abc'

#bytes(bytes_or_buffer)->immutable copy of bytes_or_buffer从一个字节序列或者buffer复制出一个新的不可变bytes
a=b'abcss'
b=bytes(a)
print(b)                #输出:b'abcss'
print(id(a))            #输出:4368131856
print(id(b))            #输出:4368131856
#说明以上复制并不是重新开辟一块内存空间,而是同时指向这块内存空间

#使用b前缀定义
print(b'asd')           #输出:b'asd'
print(b'\x41\x61')      #输出:b'Aa'

bytes操作

#bytes和str类型类似,均为不可变,很多方法一样
print(b'abcsda'.replace(b'a',b'z'))    #输出:b'zbcsdz'
print(b'abc'.find(b'b'))               #输出:1

#类方法(string必须是2个字符的16进制形式,空格被忽略)
print(bytes.fromhex('6162 09 6a 6b00'))     #b'ab\tjk\x00'

#hex(),返回16进制表示的字符串
print('abc'.encode().hex())             #输出:616263        

#索引
print(b'abcdef'[2])                     #输出:99

bytearray初始化

#空bytearray
print(bytearray())              #输出:bytearray(b'')

#bytearry(int)指定字节的bytearray,被0填充
print(bytearray(5))             #输出:bytearray(b'\x00\x00\x00\x00\x00')

#bytearray(iterable_of_ints)->bytearray
print(bytearray([0,255]))       #输出:bytearray(b'\x00\xff')

#bytearray(string,encoding[,errors])->bytearray近似string.encode()
print(bytearray('abc',encoding='utf8')) #输出bytearray(b'abc')

#bytearray(bytes_or_buffer)->immutable copy of bytes_or_buffer从一个字节序列或者buffer复制出一个新的可变的bytearray对象
a=b'abcss'
b=bytearray(a)
print(b)                #输出:bytearray(b'abcss')
print(id(a))            #输出:4368131856
print(id(b))            #输出:4362498768

bytearray操作

#和bytes类型的方法相同
print(bytearray(b'abcsda').replace(b'a',b'z'))    #输出:bytearray(b'zbcsdz')
print(bytearray(b'abc').find(b'b'))               #输出:1

#类方法(string必须是2个字符的16进制形式,空格被忽略)
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])                     #输出:99

#append(int)尾部追加一个元素
b=bytearray()
b.append(97)
b.append(99)
print(b)                    #输出:bytearray(b'ac')

#insert(index,int)在指定索引位置插入元素
b.insert(1,98)
print(b)                    #输出:bytearray(b'abc')

#extend(iterable_of_ints)将一个可迭代的整数集合追加到当前bytearray
a=[65,66,67]
b.extend(a)
print(b)                    #输出:bytearray(b'abcABC')

#pop(index=-1)指定索引上移除元素,默认从尾部删除
b.pop()
print(b)                    #输出:bytearray(b'abcAB')

#remove(value)找到第一个value移除,找不到抛异常
b.remove(66)
print(b)                    #输出:bytearray(b'abcAB')

#reverse()翻转
b.reverse()
print(b)                    #输出:bytearray(b'Acba')

#clear()清空bytearray
b.clear()
print(b)                    #输出:bytearray(b'')

编码与解码

#字符串按照不同的字符编码encode返回字节序列bytes
print('abc'.encode(encoding='utf8',errors='strict'))   #输出:b'abc'

#字节序列按照不同的字符集解码decode返回字符串
print(b'abc'.decode(encoding="utf-8",errors='strict')) #输出:abc
print(bytearray(b'abc').decode(encoding="utf-8",errors='strict'))  #输出:abc

猜你喜欢

转载自www.cnblogs.com/wuyanzu123/p/9447912.html
今日推荐