我想删除bytes
函数中参数的元素。我希望更改参数,而不是返回新对象。
def f(b: bytes):
b.pop(0) # does not work on bytes
del b[0] # deleting not supported by _bytes_
b = b[1:] # creates a copy of b and saves it as a local variable
io.BytesIO(b).read(1) # same as b[1:]
这里的解决方案是什么?
只需使用一个bytearray
:
>>> a = bytearray(b'abcdef')
>>> del a[1]
>>> a
bytearray(b'acdef')
它几乎就像bytes
但可变的:
的
bytearray
类是整数的范围为0一个可变的序列<= X <256它已大部分的可变序列的常规方法,在所描述的可变序列类型,以及大多数方法,该bytes
类型有,见字节和ByteArray操作.
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句