001_bytearray
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的更多相关文章
随机推荐
- EASYUI+MVC4通用权限管理平台
通用权限案例平台在经过几年的实际项目使用,并取得了不错的用户好评.在平台开发完成后,特抽空总结一下平台知识,请各位在以后的时间里,关注博客的更新. 1.EASYUI+MVC4通用权限管理平台--前言 ...
- 前端JS对后台传递的timestamp的转换
后台传递的timestamp类型的数据的JSON: Date.prototype.format = function(format) { var o = { "M+" : this ...
- <pages validateRequest="false"/>在.net4.0中无效的问题
再web.config中设置<pages validateRequest="false"/>在.net4.0中无效的问题 解决方案: <system.web> ...
- The resource could not be loaded because the App Transport
Xcode7 beta 网络请求报错:The resource could not be loaded because the App Transport Xcode7 beta 网络请求报错:The ...
- error: qrc_qml.obj: requires unsupported dynamic reloc R_ARM_REL32; recompile with -fPIC解决办法
使用qtcreator加androidndk编译项目时报错: error: qrc_qml.obj: requires unsupported dynamic reloc R_ARM_REL32; r ...
- Java [Leetcode 160]Intersection of Two Linked Lists
题目描述: Write a program to find the node at which the intersection of two singly linked lists begins. ...
- 省常中模拟 Test3 Day1
tile 贪心 题意:给出一个矩形,用不同字母代表的正方形填充,要求相邻的方块字母不能相同,求字典序(将所有行拼接起来)最小的方案. 初步解法:一开始没怎么想,以为策略是每次填充一个尽量大的正方形.但 ...
- cimge 这次能够图片大小尺寸
CImage imSrc,imDest;imSrc.Load(……);//读入原始图片imDest.Create(……)//创建新大小的图片imSrc.StretchBlt(imDest.GetDC( ...
- 转载RabbitMQ入门(4)--路由
路由 (使用Java客户端) 在先前的指南中,我们建立了一个简单的日志系统.我们可以将我们的日志信息广播到多个接收者. 在这部分的指南中,我们将要往其中添加一个功能-让仅仅订阅一个消息的子集成为可能. ...
- Kotlin 语言高级安卓开发入门
过去一年,使用 Kotlin 来为安卓开发的人越来越多.即使那些现在还没有使用这个语言的开发者,也会对这个语言的精髓产生共鸣,它给现在 Java 开发增加了简单并且强大的范式.Jake Wharton ...