pythonbytearray拼接_Python内置函数—bytearray
英⽂⽂档:
classbytearray([source[, encoding[, errors]]])
Return a new array of bytes. The bytearray class is a mutable sequence of integers in the range 0 <= x < 256. It has most of the usual methods of mutable sequences, described in Mutable Sequence Types, as well as most methods that
the bytes type has, see Bytes and Bytearray Operations.
The optional source parameter can be used to initialize the array in a few different ways:
If it is a string, you must also give the encoding (and optionally, errors) parameters; bytearray() then converts the string to bytes de().
If it is an integer, the array will have that size and will be initialized with null bytes.
If it is an object conforming to the buffer interface, a read-only buffer of the object will be used to initialize the bytes array.
If it is an iterable, it must be an iterable of integers in the range 0 <= x
def __init__(self, source=None, encoding=None, errors='strict'): #known special case of bytearray.__init__
"""bytearray(iterable_of_ints) -> bytearray
bytearray(string, encoding[, errors]) -> bytearray
bytearray(bytes_or_buffer) -> mutable copy of bytes_or_buffer
bytearray(int) -> bytes array of size given by the parameter initialized with null bytes
bytearray() -> empty bytes array
Construct a mutable bytearray object from:
- an iterable yielding integers in range(256)
- a text string encoded using the specified encoding
- a bytes or a buffer object
- any object implementing the buffer API.
- an integer
# (copied from class doc)"""
pass
bytearray
返回⼀个新的字节数组。
bytearray类是range 0 < = x < 256的⼀个可变序列。
它有⼤多数可变序列的常⽤⽅法,在可变序列类型中描述,以及⼤多数字节类型的⽅法,参见字节和Bytearray操作。
可选的源参数可以⽤⼏种不同的⽅式来初始化数组:
如果它是⼀个字符串,那么您还必须给出编码(以及可选的错误)参数;bytearray()然后使⽤de()将字符串转换为字节。
如果它是⼀个整数,那么数组将具有这个⼤⼩,并将⽤null字节初始化。
如果它是符合缓冲区接⼝的对象,则将使⽤对象的只读缓冲区来初始化字节数组。
如果它是可迭代的,那么它必须是range 0 < = x < 256的整数的迭代,它被⽤作数组的初始内容
如果没有参数,则创建⼀个⼤⼩为0的数组。
说明:
1. 返回值为⼀个新的字节数组
2. 当3个参数都不传的时候,返回长度为0的字节数组
#!/usr/bin/python3
b = bytearray()
print(b)
print(len(b))
结果:
bytearray(b'')
3. 当source参数为字符串时,encoding参数也必须提供,函数将字符串使⽤de⽅法转换成字节数组
b = bytearray('中⽂')
结果:
Traceback (most recent call last):
File "D:/py/day001/day1/test.py", line 3, in
b = bytearray('中⽂')
TypeError: string argument without an encoding
encoding参数也必须提供,函数将字符串使⽤de⽅法转换成字节数组
b = bytearray('中⽂', 'utf-8')
print(b)
print(len(b))
结果:
bytearray(b'\xe4\xb8\xad\xe6\x96\x87')
6
4. 当source参数为整数时,返回这个整数所指定长度的空字节数组
#!/usr/bin/python3
b = bytearray(5)
print(b)
print(len(b))
执⾏结果:
bytearray(b'\x00\x00\x00\x00\x00')
5
python获取数组长度
5. 当source参数为实现了buffer接⼝的object对象时,那么将使⽤只读⽅式将字节读取到字节数组后返回
#!/usr/bin/python3
b = bytearray([1,2,3,4,5])
print(b)
print(len(b))
执⾏结果:
bytearray(b'\x01\x02\x03\x04\x05')
5
6. 当source参数是⼀个可迭代对象,那么这个迭代对象的元素都必须符合0 <= x < 256,以便可以初始化到数组⾥#!/usr/bin/python3
b = bytearray([1,2,3,4,5,256])
print(b)
print(len(b))
执⾏结果:
Traceback (most recent call last):
File "D:/py/day001/day1/test.py", line 3, in
b = bytearray([1,2,3,4,5,256])
ValueError: byte must be in range(0, 256)

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。