bytes
bytes是Python 3中特有的(bytes及bytes;str是str),Python 2 里的数据是不区分bytes和str(str和bytes都是bytes;unicode是unicode)。
Python 2中 >>> type(b'xxxxx') <type 'str'> >>> type('xxxxx') <type 'str'> Python 3中 >>> type(b'xxxxx') <class 'bytes'> >>> type('xxxxx') <class 'str'>
区别
-
Python3中,bytes是byte的序列,而str是unicode的序列。
-
str 使用encode方法转化为 bytes
-
bytes通过decode转化为str
str转换成bytes: In [9]: str1='人生苦短,我用Python!' In [10]: type(str1) Out[10]: str In [11]: b=str1.encode() In [12]: b Out[12]: b'\xe4\xba\xba\xe7\x94\x9f\xe8\x8b\xa6\xe7\x9f\xad\xef\xbc\x8c\xe6\x88\x91\xe7\x94\xa8Python!' In [13]: type(str1.encode()) Out[13]: bytes bytes转换成str: In [22]: b Out[22]: b'\xe4\xba\xba\xe7\x94\x9f\xe8\x8b\xa6\xe7\x9f\xad\xef\xbc\x8c\xe6\x88\x91\xe7\x94\xa8Python!' In [23]: type(b) Out[23]: bytes In [24]: b.decode() Out[24]: '人生苦短,我用Python!' In [25]: type(b.decode()) Out[25]: str
注意点:
在Python 2中由于不区分str和bytes所以可以直接通过encode()和decode()方法进行编码解码。而在Python 3中把两者给分开了这个在使用中需要注意。
实际应用中在互联网上都是通过二进制进行传输,所以就需要将str转换成bytes进行传输,而在接收中通过decode()解码成我们需要的要进行处理的数据。这样不管对方是什么字符编码全部编码转成了bytes数据进行网络传输,而本地我们接收,再按照我们使用的字符编码进行解码,这样就不会乱码。
Bytearray
bytearray和bytes不一样的地方在于,bytearray是可变的。
和字符串一样,字节类型也是不可变序列,而字节数组是可变版本的字节,它们的关系就相当于list
与tuple
。由于和字符串一样是序列类型,字节和字节数组可用的方法基本上一致。
官方文档: 英文文档: class bytearray([source[, encoding[, errors]]]) Return a new array of bytes. The bytearray class is a mutable sequence of integers in the range 0 <= x < 256. It has most of the usual methods of
mutable sequences, described in Mutable Sequence Types, as well as most methods that the bytes type has, see Bytes and Bytearray Operations. The optional source parameter can be used to initialize the array in a few different ways: If it is a string, you must also give the encoding (and optionally, errors) parameters; bytearray() then converts the string to bytes using str.encode(). If it is an integer, the array will have that size and will be initialized with null bytes. If it is an object conforming to the buffer interface, a read-only buffer of the object will be used to initialize the bytes array. If it is an iterable, it must be an iterable of integers in the range 0 <= x < 256, which are used as the initial contents of the array. Without an argument, an array of size 0 is created. 译文: 返回一个新的字节数组。bytearray类是一个值域为0 <= x < 256的可变整数序列。它具有大多数可变序列的常用方法,这将在可变序列类型中进行描述,其大多方法与bytes类型相同,详细参考
bytes和bytesarray操作。 可选source参数可通过几种途径来初始化数组: - 如果是字符串,必须添加encoding(和可选error项)参数,然后bytearray() 通过利用str.encode()将字符串转化为字节 - 如果是整数,数组获得其大小,并用空字节进行初始化 - 如果是迭代,它必须是一个值域为0 <= x < 256的整数迭代,用于初始化数组的内容 无参数时,则创建一个大小为0的数组。
In [26]: str1 Out[26]: '人生苦短,我用Python!' In [28]: b1=bytearray(str1.encode()) In [29]: b1 Out[29]: bytearray(b'\xe4\xba\xba\xe7\x94\x9f\xe8\x8b\xa6\xe7\x9f\xad\xef\xbc\x8c\xe6\x88\x91\xe7\x94\xa8Python!') In [30]: b1.decode() Out[30]: '人生苦短,我用Python!' In [31]: b1[:6]=bytearray('生命'.encode()) In [32]: b1 Out[32]: bytearray(b'\xe7\x94\x9f\xe5\x91\xbd\xe8\x8b\xa6\xe7\x9f\xad\xef\xbc\x8c\xe6\x88\x91\xe7\x94\xa8Python!') In [33]: b1.decode() Out[33]: '生命苦短,我用Python!'
注意点:
data =
bytearray("str xxxxxxx",encoding
=
'utf-8'
)
bytearray转成的bytes数据由于是数组格式,可以计算其长度:len(data);也可以在数据之后追加数据:data.append(32);
但追加的必须是数字,他内部规则默认把数字转成字节(比对ASCII码表)
转载博客:http://www.cnblogs.com/cuchadanfan/p/5926069.html