bytes和bytearray总结

The core built-in types for manipulating binary data are bytes and bytearray. They are supported by memoryview which uses the buffer protocol to access the memory of other binary objects without needing to make a copy.

bytearray objects are a mutable counterpart to bytes objects.

bytes定义

1.使用bytes函数创建bytes

  • bytes() 创建一个空的bytes
  • bytes(int) 创建一个int位的全位0的bytes
  • bytes(iterabl_of_ints) 可迭代数字组成的bytes(比如range)
  • bytes(string,encoding[,errors]) 等价于string.encode()
  • bytes(bytes of buffer) 创建一个bytes的copy

2.直接定义

比如:

? b = b‘abc‘

? b = b‘\x61‘

3.类型转换

  • string.encode()
  • int.tobytes()
  • bytes.from

bytes函数定义

>>> bytes()
b‘‘
>>> bytes(3)
b‘\x00\x00\x00‘
>>> bytes(range(3))
b‘\x00\x01\x02‘
>>> b = bytes(‘中国‘, encoding=‘utf-8‘)
>>> b
b‘\xe4\xb8\xad\xe5\x9b\xbd‘
>>> bytes(b)
b‘\xe4\xb8\xad\xe5\x9b\xbd‘

直接创建:

>>> b = b‘abc‘
>>> b
b‘abc‘

类型转换:

>>> n = 97
>>> n.to_bytes(1,byteorder=‘big‘)
b‘a‘
>>> s = ‘中国‘

>>> s.encode(encoding = ‘utf-8‘)
b‘\xe4\xb8\xad\xe5\x9b\xbd‘

>>> bytes.fromhex(‘61‘)
b‘a‘

bytes的显示方式

Only ASCII characters are permitted in bytes literals (regardless of the declared source code encoding). Any binary values over 127 must be entered into bytes literals using the appropriate escape sequence.

只有ASCII中的字符串是可以直接在bytes类型中显示出来的,所有大于127的数值用转义字符表达。

比如,内存中的字节对象用十六进制表示为61,在python中显示的方式不是b‘\x61‘ 而是b‘a‘;而b‘\xe4‘显示方式就是b‘\xe4‘;注意:仅仅是显示方式而已

另外,并不是所有的小于127的都可以被友好的显示出来,有些对象本身不可显示,就显示其十六进制表示。比如

b‘\x00‘

bytes的一般方法

bytes类似于string;在方法上,除了自己特有的方法外,跟str也类似。

比如:

>>> b‘abc‘.find(b‘\x63‘)
2
>>> b‘abc‘.replace(b‘\61‘,b‘A‘)
b‘abc‘

bytearray定义

bytearray是可变的bytes数据类型,可以通过bytearray创建和定义

一:bytearray()定义

  • bytearray() 创建一个空的bytearray
  • bytearray(int) 创建一个int位的全位0的bytearray
  • bytearray(iterabl_of_ints) 可迭代数字组成的bytearray(比如range)
  • bytearray(string,encoding[,errors]) 将一个字符串编码为bytearray
  • bytearray(bytes of buffer) 创建一个bytearray

二: bytearray的方法定义

  • bytearray.fromhex()

bytearray的一般方法

bytearray具备bytes的操作方法,像字符串一样操作;

另外bytearray还具备像list一样的操作方法,比如pop,append等

bytes 和 bytearray的方法

十六进制和字节类型的相互转换

  • bytes.fromhex()
  • bytearray.fromhex()
  • bytes.hex()
  • bytesarray.hex()
>>> b = bytes(‘hell‘,encoding=‘utf-8‘)
>>> ba = bytearray(‘hell‘,encoding=‘utf-8‘)
>>> b
b‘hell‘
>>> ba
bytearray(b‘hell‘)
>>> b.hex()
‘68656c6c‘
>>> ba.hex()
‘68656c6c‘

原文地址:https://www.cnblogs.com/dingtianwei/p/9459575.html

时间: 2024-07-31 13:33:21

bytes和bytearray总结的相关文章

python string类型 bytes类型 bytearray类型

一.python3对文本和二进制数据做了区分.文本是Unicode编码,str类型,用于显示.二进制类型是bytes类型,用于存储和传输.bytes是byte的序列,而str是unicode的序列. str类型: 1 >>> s = u'你好' 2 >>> s 3 '你好' 4 >>> type(s) 5 <class 'str'> bytes类型: 1 >>> b = b'abc' 2 >>> b 3

Python的程序结构[2] -&gt; 类/Class -&gt; 内建类 bytes 和 bytearray

内建类 bytes 和 bytearray / Built-in Type bytes and bytearray 关于内建类 Python的内建类 bytes 主要有以下几点: class bytes([source[, encoding[, errors]]]) Return a new "bytes" object, which is an immutable sequence of integers in the range 0 <= x < 256. bytes

python数据l类型 ——bytes 和 bytearray

bytes和 bytearray bytes:可以看作是一组数值(0-256)(二进制) 的 str 序列bytearray :可以看作是一组数值(0-256)(二进制) 的 list 序列 bytes类型 字符串转bytes类型 # 将返回 bytes 类型 b" abc " bs1 = bytes("abc","utf-8") # 可以使用字符的16进制字符表达形式 bs2 = bytes('\x61\x62\x63',"utf-8

内置函数:bytes、bytearray、memoryview

bytes:字节组成的有序不可变序列bytearray:在phthon3当中新增的bytearray为字节组成的可变序列,修改字节不会生成新的序列字符串以字符为单位,转换成bytes类型以字节为单位,两者并无区别,本质都是编译成计算机可以读懂的01010101字节码两者参数一样: 如果 source 为整数,则返回一个长度为 source 的初始化数组: 如果 source 为字符串,则按照指定的 encoding 将字符串转换为字节序列: 如果 source 为可迭代类型,则元素必须为[0 ,

python中bytes与bytearray以及encode与decode

一.encode与decode 1.bytes主要是给在计算机看的,string主要是给人看的 2.中间有个桥梁就是编码规则,现在大趋势是utf8 3.bytes对象是二进制,很容易转换成16进制,例如\x64 4.string就是我们看到的内容,例如'abc' 5.string经过编码encode,转化成二进制对象,给计算机识别 6.bytes经过反编码decode,转化成string,让我们看,但是注意反编码的编码规则是有范围,\xc8就不是utf8识别的范围 例如encode使用: "哈哈

bytes和bytearray

bytes bytes是Python 3中特有的,Python 2 里不区分bytes和str. Python 2中 >>> type(b'xxxxx') <type 'str'> >>> type('xxxxx') <type 'str'> Python 3中 >>> type(b'xxxxx') <class 'bytes'> >>> type('xxxxx') <class 'str'&

5——深浅复制、bytes和bytearray、linux用户

深浅复制 深浅复制只有在列表嵌套列表的情况下讨论 如果想保留修改之前的数据,就可以使用列表的复制,但要注意列表嵌套情况下的问题 l1 = [1,[2, 3]] l2 = l1.copy() #copy 浅复制 随着l1或l2的改变而发生改变 #浅复制只能改变第一层列表的数据,不能改变列表中嵌套的数据 ? import copy l3 = copy.deepcopy(l1) #深复制 能改变列表中所有嵌套的数据  bytes二进制序列类型 01 指定长度的零填充字节对象: [in] bytes(3

bytes bytearray;切片操作

bytes # 不可变字节序列 bytearray # 可变字节数组 bytes: str.encode() -> bytes  #对字符串str进行编码,()里不写内容默认以UTF-8编码 bytes.decode() -> str  #对bytes进行解码,()里不写内容默认以UTF-8解码 # 使用b前缀定义 # 只允许基本ASCII使用字符形式b'abc9' (例如b'中'不可以) # 使用16进制表示b"\x41\x61"(表示'Aa') 类方法 bytes.fr

Python内置函数——bytearray()-暂疑

英文文档: 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