Python bytearray() function returns a bytearray object that contains the array of bytes from the input source. The bytearray class is mutable, so we can change its element values.

Python bytearray()函数返回一个bytearray对象,其中包含来自输​​入源的字节数组。 bytearray类是可变的,因此我们可以更改其元素值。

Python bytearray() (Python bytearray())

Python bytearray() function syntax is:

Python bytearray()函数语法为:

class bytearray(]])

source is used to initialize the bytearray object array elements. This is an optional argument.

source用于初始化bytearray对象数组元素。 这是一个可选参数。

encoding is optional unless source is string. It’s used for converting the string to bytes using str.encode() function.

除非source是string,否则encoding是可选的。 它用于使用str.encode()函数将字符串转换为字节。

errors is optional parameter. It’s used if the source is string and encoding fails due to some error.

errors是可选参数。 如果源是字符串并且由于某些错误而编码失败,则使用它。

There are some specific rules followed by bytearray() function depending on the type of source.

根据源类型,有一些特定的规则,后跟bytearray()函数。

  • If no argument is passed, empty byte array is returned.如果未传递任何参数,则返回空字节数组。
  • If source is integer, it initializes the byte array of given length with null values.如果source是整数,则使用空值初始化给定长度的字节数组。
  • If source is string, encoding is mandatory and used to convert string to byte array.如果source是字符串,则编码是强制性的,用于将字符串转换为字节数组。
  • If source is iterable, such as list, it must be an iterable of integers in the range 0 <= x < 256, which are used as the initial contents of the array.如果source是可迭代的,例如list ,则它必须是0 <= x <256范围内的整数的可迭代对象,这些整数用作数组的初始内容。

Let’s look at some of the examples of bytearray() function.

让我们看一下bytearray()函数的一些示例。

不带参数的bytearray() (bytearray() with no arguments)

b = bytearray()
print(b)

Output:

输出:

bytearray(b'')

具有字符串和可变性的bytearray() (bytearray() with string and mutability)

# string to bytearray
# encoding is mandatory, otherwise "TypeError: string argument without an encoding"
b = bytearray('abc', 'UTF-8')
print(b)
b[1] = 65  # mutable
print(b)

Output:

输出:

bytearray(b'abc')
bytearray(b'aAc')

具有int参数的bytearray() (bytearray() with int argument)

b = bytearray(5)
print(b)

Output:

输出:

bytearray(b'\x00\x00\x00\x00\x00')

具有可迭代的bytearray() (bytearray() with iterable)

b = bytearray([1, 2, 3])
print(b)

Output:

输出:

bytearray(b'\x01\x02\x03')

That’s all for a quick guide of python bytearray() function.

这就是python bytearray()函数的快速指南。

GitHub Repository.GitHub存储库中检出完整的python脚本和更多Python示例。

Reference: Official Documentation

参考: 官方文档

翻译自: https://www.journaldev.com/22703/python-bytearray

Python bytearray()相关推荐

  1. Python reversed()函数

    Python reversed() function returns a reversed iterator from the specified sequence argument. Python ...

  2. Python eval()函数的使用

    Python eval()函数的使用 文章目录 一.官网给出的定义 1.eval() 方法的语法: 二.eval的作用 1.计算字符串中有效的表达式,并返回结果 2.将字符串转成相应的对象(如list ...

  3. Python xrange()函数

    Python xrange() function is used to generate a sequence of integers. This function is part of Python ...

  4. Python ord(),chr()函数

    Python ord() and chr() are built-in functions. They are used to convert a character to an int and vi ...

  5. Python complex()

    Python complex() function is used to create complex numbers. It's a built-in function that returns a ...

  6. Python Frozenset()

    Python frozenset is an unordered collection of distinct hashable objects. Frozenset is an immutable ...

  7. Python strftime()

    Python strftime() function is present in datetime and time modules to create a string representation ...

  8. Python raw_input()

    Good day, learners. In our previous tutorial we learned about Python SimpleHTTPServer. In this tutor ...

  9. Python sorted()函数

    Python sorted() function returns a sorted list from the items in the iterable. Python sorted()函数从ite ...

最新文章

  1. java oracle数据回滚,误操作ORACLE生产数据利用闪回查询备份恢复数据-java-51CTO博客...
  2. 堆和栈的区别 (转贴)
  3. ECshop网点程序优化-后台添加类目自动选择上次父类目并计算Sort Order
  4. 文献阅读6-Entity-Relation Extraction as Multi-turn Question Answering(实体关系联合抽取,层次标签依赖关系,multi-turn QA)
  5. JavaScript:JavaScript语法的详细介绍
  6. java 蓝桥杯算法提高 成绩排序2
  7. LeetCode 289. 生命游戏
  8. 从spring管理的datasource中获取connection
  9. 老罗Android开发视频教程 (android常用布局介绍)5集集合
  10. cad汉仪长仿宋体_hzcf.shx字体
  11. 图像处理——空间域和频率域部分图像增强学习
  12. 安装office2007 1706错误
  13. 颂钵带给我们是什么感受
  14. 3d稀疏卷积——spconv源码剖析(一)
  15. 基于SSM的在线测试及评卷系统
  16. Linux系统中read的用法,Linux系统中read的使用技巧
  17. Spring学习笔记(完结)
  18. 计算机excel操作教程,Excel操作教程 -电脑资料
  19. python人工智能入门零基础_【贾老坏】真·零基础入门人工智能+Python+数学
  20. KVM虚拟化技术之使用Qemu-kvm创建和管理虚拟机

热门文章

  1. KnockoutJS 3.X API 第四章 表单绑定(11) options绑定
  2. 扩展 delphi 泛型 以实现类似lambda功能 , C#中的any count first last 等扩展方法
  3. [转载] java简易爬虫Crawler
  4. 用框架名唬人谁都会,那你知道Web开发模式吗?——莫问前程莫装逼
  5. js,jquery小知识点
  6. vim 保存文件的回车换行模式
  7. Eclipse自动生成作者、日期注释等功能设置
  8. android开发学习笔记系列(6)--代码规范
  9. C#服务启动以及服务指令
  10. 常见HTTP状态(304,200等)转