python的bytearray对象的使用

2024-01-01 17:20
文章标签 python 使用 对象 bytearray

本文主要是介绍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 "<pyshell#18>", 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 "<pyshell#32>", 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 "<pyshell#59>", 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的bytearray对象的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/559975

相关文章

利用python实现对excel文件进行加密

《利用python实现对excel文件进行加密》由于文件内容的私密性,需要对Excel文件进行加密,保护文件以免给第三方看到,本文将以Python语言为例,和大家讲讲如何对Excel文件进行加密,感兴... 目录前言方法一:使用pywin32库(仅限Windows)方法二:使用msoffcrypto-too

Java Spring 中 @PostConstruct 注解使用原理及常见场景

《JavaSpring中@PostConstruct注解使用原理及常见场景》在JavaSpring中,@PostConstruct注解是一个非常实用的功能,它允许开发者在Spring容器完全初... 目录一、@PostConstruct 注解概述二、@PostConstruct 注解的基本使用2.1 基本代

C#使用StackExchange.Redis实现分布式锁的两种方式介绍

《C#使用StackExchange.Redis实现分布式锁的两种方式介绍》分布式锁在集群的架构中发挥着重要的作用,:本文主要介绍C#使用StackExchange.Redis实现分布式锁的... 目录自定义分布式锁获取锁释放锁自动续期StackExchange.Redis分布式锁获取锁释放锁自动续期分布式

springboot使用Scheduling实现动态增删启停定时任务教程

《springboot使用Scheduling实现动态增删启停定时任务教程》:本文主要介绍springboot使用Scheduling实现动态增删启停定时任务教程,具有很好的参考价值,希望对大家有... 目录1、配置定时任务需要的线程池2、创建ScheduledFuture的包装类3、注册定时任务,增加、删

使用Python实现矢量路径的压缩、解压与可视化

《使用Python实现矢量路径的压缩、解压与可视化》在图形设计和Web开发中,矢量路径数据的高效存储与传输至关重要,本文将通过一个Python示例,展示如何将复杂的矢量路径命令序列压缩为JSON格式,... 目录引言核心功能概述1. 路径命令解析2. 路径数据压缩3. 路径数据解压4. 可视化代码实现详解1

python获取网页表格的多种方法汇总

《python获取网页表格的多种方法汇总》我们在网页上看到很多的表格,如果要获取里面的数据或者转化成其他格式,就需要将表格获取下来并进行整理,在Python中,获取网页表格的方法有多种,下面就跟随小编... 目录1. 使用Pandas的read_html2. 使用BeautifulSoup和pandas3.

Pandas透视表(Pivot Table)的具体使用

《Pandas透视表(PivotTable)的具体使用》透视表用于在数据分析和处理过程中进行数据重塑和汇总,本文就来介绍一下Pandas透视表(PivotTable)的具体使用,感兴趣的可以了解一下... 目录前言什么是透视表?使用步骤1. 引入必要的库2. 读取数据3. 创建透视表4. 查看透视表总结前言

Python装饰器之类装饰器详解

《Python装饰器之类装饰器详解》本文将详细介绍Python中类装饰器的概念、使用方法以及应用场景,并通过一个综合详细的例子展示如何使用类装饰器,希望对大家有所帮助,如有错误或未考虑完全的地方,望不... 目录1. 引言2. 装饰器的基本概念2.1. 函数装饰器复习2.2 类装饰器的定义和使用3. 类装饰

Python 交互式可视化的利器Bokeh的使用

《Python交互式可视化的利器Bokeh的使用》Bokeh是一个专注于Web端交互式数据可视化的Python库,本文主要介绍了Python交互式可视化的利器Bokeh的使用,具有一定的参考价值,感... 目录1. Bokeh 简介1.1 为什么选择 Bokeh1.2 安装与环境配置2. Bokeh 基础2

Android使用ImageView.ScaleType实现图片的缩放与裁剪功能

《Android使用ImageView.ScaleType实现图片的缩放与裁剪功能》ImageView是最常用的控件之一,它用于展示各种类型的图片,为了能够根据需求调整图片的显示效果,Android提... 目录什么是 ImageView.ScaleType?FIT_XYFIT_STARTFIT_CENTE