bytearray([source [, encoding [, errors]]])

中文说明:

bytearray([source [, encoding [, errors]]])返回一个byte数组。Bytearray类型是一个可变的序列,并且序列中的元素的取值范围为 [0 ,255]。

参数source:

如果source为整数,则返回一个长度为source的初始化数组;

如果source为字符串,则按照指定的encoding将字符串转换为字节序列;

如果source为可迭代类型,则元素必须为[0 ,255]中的整数;

如果source为与buffer接口一致的对象,则此对象也可以被用于初始化bytearray.。

版本:在python2.6后新引入,在python3中同样可以使用

In[2]: a = bytearray(3)
In[3]: a
Out[3]: bytearray(b'\x00\x00\x00')
In[4]: a[0]
Out[4]: 0
In[5]: a[1]
Out[5]: 0
In[6]: a[2]
Out[6]: 0
In[7]: b = bytearray("abc")
Traceback (most recent call last):
File "/root/python_dev/.pyenv/versions/3.4.4/lib/python3.4/site-packages/IPython/core/interactiveshell.py", line 2885, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-7-e2c0db524cd1>", line 1, in <module>
b = bytearray("abc")
TypeError: string argument without an encoding
In[8]: b = bytearray(b"abc")
In[9]: b
Out[9]: bytearray(b'abc')
In[10]: b[0]
Out[10]: 97
In[11]: b[1]
Out[11]: 98
In[12]: b[2]
Out[12]: 99
In[13]: c = bytearray([1, 2, 3])
In[14]: c
Out[14]: bytearray(b'\x01\x02\x03')
In[15]: c[0]
Out[15]: 1
In[16]: c[1]
Out[16]: 2
In[17]: c[2]
Out[17]: 3

001_bytearray的更多相关文章

随机推荐

  1. IIS与ASP.NET中的队列

    一.IIS:应用程序池队列(Application pool queue,位于HTTP.SYS) 这是请求到达IIS后遇到的第一个队列,http.sys收到请求后会将请求放入对应的应用程序池队列,这样 ...

  2. linux 服务自动调用

    php服务地址: http://192.168.2.117/web/index.php/buy/auctionselect 编写脚本service.sh,内容: #! /bin/sh crul htt ...

  3. Java [Leetcode 268]Missing Number

    题目描述: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is ...

  4. Java [leetcode 4] Median of Two Sorted Arrays

    问题描述: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of t ...

  5. dpkg-query

    1.功能作用 查看软件包信息 2.位置 /usr/bin 3.格式用法 dpkg-query [<选项> ...] <命令> 4.主要参数 Commands: -s|--sta ...

  6. dos文件批量转换成unix文件

    对于经常在windows环境下和linux环境同时使用的文件(如在windows系统下编写,在linux环境下编译的文件), 常常存在这样的问题:由于两种系统的格式文件格式不同,导致程序出现不期望的问 ...

  7. win7/8下VirtualBox虚拟共享文件夹设置

    1. 安装增强功能包(VBoxGuestAdditions)   打开虚拟机,运行ubuntu,在菜单栏选择"设备->安装增强功能",根据提示即可安装成功(成功后也可 以实现 ...

  8. Oracle DBA 的常用Unix参考手册(二)

    9.AIX下显示CPU数量    # lsdev -C|grep Process|wc -l10.Solaris下显示CPU数量# psrinfo -v|grep "Status of pr ...

  9. 在英文 sql2005中 比较nvarchar 与 varchar的速度

    declare @str1 varchar(max); declare @count int; ; print 'begin' begin set @str1 = @str1 + '*'; ; end ...

  10. 【Android】以BaseAdapter做适配器的ListView及其性能优化

    适配器的Java类 package com.app.adapter; import org.json.JSONArray; import org.json.JSONObject; import and ...