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的更多相关文章
随机推荐
- bzoj2790
观察这道题,d(a,b) 就是先变成最大公约数然后再变成b 设g[x]表示x的质因数数目,不难得到d(a,b)=g[a/gcd(a,b)]+g[b/gcd(a,b)] 因为g[xy]=g[x]+g[y ...
- Sublime-text markdown with Vim mode and auto preview
说明 最近看到markdown相关的东西,被其书写方式吸引,其实以前就在找这种类似的工具,但是也没找到,由于习惯了Vim,可Vim不支持markdown预览,这点可能不是很好,于是找到Sublime- ...
- angular js 实例参数学习
<!DOCTYPE html> <html> <head> <meta name="description" content=" ...
- Java [Leetcode 223]Rectangle Area
题目描述: Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is def ...
- Java [Leetcode 225]Implement Stack using Queues
题目描述: Implement the following operations of a stack using queues. push(x) -- Push element x onto sta ...
- 在Windows下编译ffmpeg完全手册
本文的内容几乎全部来自于FFmpeg on Windows,但是由于国内的网络封锁,很难访问这个域名下的内容,因此我一方面按照我自己的理解和实践做了翻译,另一方面也是为了能提供一个方便的参考方法. 注 ...
- 【WEB】jsp向servlet传参中文乱码问题解决
传参方式:POST.GET.link方式 servlet向jsp传中文参数msg if(username.equals("") || password.euqals("& ...
- Asp.Net Core(.net内核)
Asp.Net Core(.net内核) //----------------Day1----------------一章 Web基本原理 1节课程说明 web窗体--设计界面--加法使用Chr ...
- spring中的BeanFactory与ApplicationContext的作用和区别?
BeanFactory类关系继承图 1. BeanFactory类结构体系: BeanFactory接口及其子类定义了Spring IoC容器体系结构,由于BeanFactory体系非常的庞大和复杂, ...
- HDU 4539 郑厂长系列故事——排兵布阵
http://acm.hdu.edu.cn/showproblem.php?pid=4539 郑厂长系列故事——排兵布阵 Time Limit: 10000/5000 MS (Java/Others) ...