python3:字节与字节数组,以及与字符串的相互转换 [英] python3: bytes vs bytearray, and converting to and from strings

查看:53
本文介绍了python3:字节与字节数组,以及与字符串的相互转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解 python3 的 bytesbytearray 类.我看过关于它们的文档,但没有全面描述它们的差异以及它们如何与 string 对象交互.

解决方案

bytes 和 bytearrays 类似...

python3 的 bytesbytearray 类都保存字节数组,其中每个字节可以取 0 到 255 之间的值.主要区别在于 bytes 对象是immutable,这意味着一旦创建,你就不能修改它的元素.相比之下,bytearray 对象允许您修改其元素.

字节和编码

字节对象可以通过几种不同的方式构建:

<预><代码>>>>字节(5)b'\x00\x00\x00\x00\x00'>>>字节([97, 98, 99])b'abc'>>>b'abc'b'abc'>>>字节('abc')类型错误:没有编码的字符串参数>>>'abc'.encode('utf-8')b'abc'>>>字节('abc','utf-8')b'abc'>>>'abc'.encode('utf-16')b'\xff\xfea\x00b\x00c\x00'>>>'abc'.encode('utf-16-le')b'a\x00b\x00c\x00'

注意最后两个的区别:'utf-16'指定了通用的utf-16编码,因此它的编码形式包括一个两字节的字节顺序标记".前言[0xff, 0xfe].当指定 'utf-16-le' 的显式排序时后一个例子,编码形式省略了字节顺序标记.

因为 bytes 对象是不可变的,所以试图改变它的一个元素导致错误:

<预><代码>>>>a = bytes('abc', 'utf-8')>>>一种b'abc'>>>[1] = 102类型错误:'bytes' 对象不支持项目分配

字节数组和编码

bytes一样,字节数组可以通过多种方式构建:

<预><代码>>>>字节数组(5)bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')>>>bytearray([1, 2, 3])bytearray(b'\x01\x02\x03')>>>bytearray('abc')类型错误:没有编码的字符串参数>>>bytearray('abc', 'utf-8')字节数组(b'abc')>>>bytearray('abc', 'utf-16')bytearray(b'\xff\xfea\x00b\x00c\x00')>>>bytearray('abc', 'utf-16-le')bytearray(b'a\x00b\x00c\x00')

因为字节数组是可变的,你可以修改它的元素:

<预><代码>>>>a = bytearray('abc', 'utf-8')>>>一种字节数组(b'abc')>>>[1]=114>>>一种字节数组(b'弧')

附加字节和字节数组

bytesbytearray 对象可以用 + 运算符连接起来:

<预><代码>>>>a = 字节(3)>>>一种b'\x00\x00\x00'>>>b = 字节数组(4)>>>乙bytearray(b'\x00\x00\x00\x00')>>>a+bb'\x00\x00\x00\x00\x00\x00\x00'>>>b+abytearray(b'\x00\x00\x00\x00\x00\x00\x00')

请注意,连接的结果采用第一个参数的类型,因此 a+b 生成一个 bytes 对象和 b+a产生一个 bytearray.

将字节和字节数组对象转换为字符串

bytes 和 bytearray 对象可以使用 decode 函数转换为字符串.该函数假定您提供与编码类型相同的解码类型.例如:

<预><代码>>>>a = bytes('abc', 'utf-8')>>>一种b'abc'>>>a.decode('utf-8')'ABC'>>>b = bytearray('abc', 'utf-16-le')>>>乙bytearray(b'a\x00b\x00c\x00')>>>b.decode('utf-16-le')'ABC'

I'd like to understand about python3's bytes and bytearray classes. I've seen documentation on them, but not a comprehensive description of their differences and how they interact with string objects.

解决方案

bytes and bytearrays are similar...

python3's bytes and bytearray classes both hold arrays of bytes, where each byte can take on a value between 0 and 255. The primary difference is that a bytes object is immutable, meaning that oncce created, you cannot modify its elements. By contrast, a bytearray object allows you to modify its elements.

bytes and encoding

A bytes object can be constructed in a few different ways:

>>> bytes(5)
b'\x00\x00\x00\x00\x00'

>>> bytes([97, 98, 99])
b'abc'

>>> b'abc'
b'abc'

>>> bytes('abc')
TypeError: string argument without an encoding

>>> 'abc'.encode('utf-8')
b'abc'

>>> bytes('abc', 'utf-8')
b'abc'

>>> 'abc'.encode('utf-16')
b'\xff\xfea\x00b\x00c\x00'

>>> 'abc'.encode('utf-16-le')
b'a\x00b\x00c\x00'

Note the difference between the last two: 'utf-16' specifies a generic utf-16 encoding, so its encoded form includes a two-byte "byte order marker" preamble of [0xff, 0xfe]. When specifying an explicit ordering of 'utf-16-le' as in the latter example, the encoded form omits the byte order marker.

Because a bytes object is immutable, attempting to change one of its elements results in an error:

>>> a = bytes('abc', 'utf-8')
>>> a
b'abc'
>>> a[1] = 102
TypeError: 'bytes' object does not support item assignment

bytearray and encoding

Like bytes, a bytearray can be constructed in a number of ways:

>>> bytearray(5)
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')

>>>bytearray([1, 2, 3])
bytearray(b'\x01\x02\x03')

>>> bytearray('abc')
TypeError: string argument without an encoding

>>> bytearray('abc', 'utf-8')
bytearray(b'abc')

>>> bytearray('abc', 'utf-16')
bytearray(b'\xff\xfea\x00b\x00c\x00')

>>> bytearray('abc', 'utf-16-le')
bytearray(b'a\x00b\x00c\x00')

Because a bytearray is mutable, you can modify its elements:

>>> a = bytearray('abc', 'utf-8')
>>> a
bytearray(b'abc')
>>> a[1]=114
>>> a
bytearray(b'arc')

appending bytes and bytearrays

bytes and bytearray objects may be catenated with the + operator:

>>> a = bytes(3)
>>> a
b'\x00\x00\x00'

>>> b = bytearray(4)
>>> b
bytearray(b'\x00\x00\x00\x00')

>>> a+b
b'\x00\x00\x00\x00\x00\x00\x00'

>>> b+a
bytearray(b'\x00\x00\x00\x00\x00\x00\x00')

Note that the catenated result takes on the type of the first argument, so a+b produces a bytes object and b+a produces a bytearray.

converting bytes and bytearray objects into strings

bytes and bytearray objects can be converted to strings using the decode function. The function assumes that you provide the same decoding type as the encoding type. For example:

>>> a = bytes('abc', 'utf-8')
>>> a
b'abc'
>>> a.decode('utf-8')
'abc'

>>> b = bytearray('abc', 'utf-16-le')
>>> b
bytearray(b'a\x00b\x00c\x00')
>>> b.decode('utf-16-le')
'abc'

这篇关于python3:字节与字节数组,以及与字符串的相互转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆