python的bytearray对象的使用

1 python的bytearray对象的使用

python3.x的bytearry也属于字符串类型,与bytes类似,属于字节串,每个字节都是一个整数,范围[0,255],但是bytesarry属于可以原处修改的字节串类型。

1.1 创建bytearray对象

1.1.1 通过bytearray(bytes)创建

用法

bytearray(bytes)

描述

bytes:为bytes对象,可以是字面值创建,也可以’字符串’.encode()创建。

示例

# 通过bytearray(bytes)创建
# 字面值创建bytes后传入
>>> ba=bytearray(b'python')
>>> ba,type(ba)
(bytearray(b'python'), <class 'bytearray'>)
# 字面值只能创建ASCII字符
>>> bytearray(b'梯')
SyntaxError: bytes can only contain ASCII literal characters.
# ‘字符串’.encode()创建bytes后传入
>>> ba=bytearray('梯'.encode('gbk'))
>>> ba,type(ba)
(bytearray(b'\xcc\xdd'), <class 'bytearray'>)

1.1.2 通过bytearray(str,encoding)创建

用法

bytearray(str,encoding)

描述

str:字符串;

encoding:编码名称;

示例

>>> ba=bytearray('梯',encoding='gbk')
>>> ba,type(ba)
(bytearray(b'\xcc\xdd'), <class 'bytearray'>)

1.1.3 通过bytearray(可迭代整数序列)创建

用法

bytearray(iterable_of_ints)

描述

iterable_of_ints:为可迭代整数序列,每个整数元素范围[0,255]

示例

>>> ba=bytearray((204,221))
>>> ba,type(ba)
(bytearray(b'\xcc\xdd'), <class 'bytearray'>)
# 整数序列的整数范围[0,255]
>>> ba=bytearray((204,256))
Traceback (most recent call last):
  File "", line 1, in <module>
    ba=bytearray((204,256))
ValueError: byte must be in range(0, 256)

1.1.4 通过bytearray(int)创建

用法

bytearray(int)

描述

创建一个指定长度的bytearray字节数组,元素默认为\x00.

int:为数组长度;

示例

>>> ba=bytearray(3)
>>> ba,type(ba)
(bytearray(b'\x00\x00\x00'), <class 'bytearray'>)

1.2 操作bytearray对象

bytearray对象的操作,原处修改原对象。

1.2.1 append(int)

用法

bytearray().append(int)

描述

在bytearray对象的尾部添加一个元素,元素范围[0,255],会修改原对象。

示例

>>> ba=bytearray('梯'.encode('gbk'))
>>> ba
bytearray(b'\xcc\xdd')
>>> ba.append(12)
>>> ba
bytearray(b'\xcc\xdd\x0c')
>>> ba.append(256)
Traceback (most recent call last):
  File "", line 1, in <module>
    ba.append(256)
ValueError: byte must be in range(0, 256)

1.2.2 insert(index,int)

用法

bytearray().insert(index,int)

描述

在bytearray对象的指定索引位置插入元素,会修改原对象。

示例

>>> ba=bytearray('梯'.encode('gbk'))
>>> ba
bytearray(b'\xcc\xdd')
>>> ba.insert(0,12)
>>> ba
bytearray(b'\x0c\xcc\xdd')

1.2.3 extend(iterable_of_ints)

用法

bytearray().extend(iterable_of_ints)

描述

在bytearray对象末尾添加整数组成的可迭代序列,会原处修改对象。

示例

>>> ba=bytearray(b'python')
>>> ba
bytearray(b'python')
>>> ba.extend((12,15))
>>> ba
bytearray(b'python\x0c\x0f')

1.2.4 pop(index=-1)

用法

bytearray().pop(index=-1)

描述

删除并返回指定索引位置的barray元素。默认删除最后一个。

示例

>>> ba=bytearray('梯阅线条','gbk')
>>> ba
bytearray(b'\xcc\xdd\xd4\xc4\xcf\xdf\xcc\xf5')
>>> ba.pop()
245
>>> ba
bytearray(b'\xcc\xdd\xd4\xc4\xcf\xdf\xcc')
>>> ba.pop(1)
221
>>> ba
bytearray(b'\xcc\xd4\xc4\xcf\xdf\xcc')

1.2.5 remove(value)

用法

bytearray().remove(value)

描述

删除bytearray指定值的元素,若不存在则报值错误。value为整数。

示例

>>> ba=bytearray('梯阅线条','gbk')
>>> ba
bytearray(b'\xcc\xdd\xd4\xc4\xcf\xdf\xcc\xf5')
>>> int('dd',16)
221
>>> ba.remove(221)
>>> ba
bytearray(b'\xcc\xd4\xc4\xcf\xdf\xcc\xf5')
>>> ba.remove(221)
Traceback (most recent call last):
  File "", line 1, in <module>
    ba.remove(221)
ValueError: value not found in bytearray
>>> ba=bytearray(b'python')
>>> ba
bytearray(b'python')
>>> list((i for i in ba))
[121, 116, 104, 111, 110]
>>> ba.remove(112)
>>> ba
bytearray(b'ython')

1.2.6 clear()

用法

bytearray().clear()

描述

清空bytearray对象的元素。

示例

>>> ba=bytearray('梯阅线条','gbk')
>>> ba
bytearray(b'\xcc\xdd\xd4\xc4\xcf\xdf\xcc\xf5')
>>> ba.clear()
>>> ba
bytearray(b'')
>>> ba.clear()
>>> ba
bytearray(b'')

1.2.7 reverse()

用法

bytearray().reverse()

描述

翻转bytearray对象的元素顺序。

示例

>>> ba=bytearray('梯阅','gbk')
>>> ba
bytearray(b'\xcc\xdd\xd4\xc4')
>>> ba.reverse()
>>> ba
bytearray(b'\xc4\xd4\xdd\xcc')

1.3 [i]访问bytearray对象

用法

bytearray()[index]

描述

通过索引访问bytearray对象的元素,index为索引编号。

示例

>>> ba=bytearray('梯阅','gbk')
>>> ba
bytearray(b'\xcc\xdd\xd4\xc4')
>>> ba[0]
204

1.4 list(bytearray)

描述

通过list(bytearray)获取bytearray对象的int序列

示例

>>> b='梯'.encode('utf-8')
>>> c='梯'
>>> ba=bytearray('梯','utf-8')
>>> b,c,ba
(b'\xe6\xa2\xaf', '梯', bytearray(b'\xe6\xa2\xaf'))
>>> list(b)
[230, 162, 175]
>>> list(c)
['梯']
>>> list(ba)
[230, 162, 175]

你可能感兴趣的:(python,python)