bytes和bytearray

python - Bytes和Bytearray

标签(空格分隔): python-数据结构


bytes、bytearray

  • [x] python3 引入两个新的类型

    • [x] bytes
      • 不可变字节序列
    • [x] bytearry
      • 字节数组
      • 可变
  • [x] 字符串与bytes

    • 字符串是字符组成的有序序列,字符可以使用编码来理解
    • bytes是字节组成的有序的不可变序列
    • bytearray是字节组成的有序的可变序列
  • [x] 编码和解码

    • 字符串按照不同的字符集编码 encode 返回字节序列 bytes
      • encode(encoding="utf-8", error="strict") --> bytes
    • 字节序列按照不同的字符集解码 decode 返回字符串
      • bytes.decode(encoding="utf-8", error="strict") --> str
      • bytearray.decode(encoding="utf-8", error="strict") --> str

  • [x] bytes定义

    • bytes() 空 bytes
    • bytes(int) 指定字节的bytes,被 0 填充
    • bytes(iterable_of_in) --> bytes [0, 255] 的int组成的可迭代对象
    • bytes(string, encoding[, errors]) -> bytes 等价于 string.encode()
    • bytes(bytes_or_buffer) -> immutable copy of bytes_or_buffer 从一个字节序列或者buffer复制除一个新的不可变的bytes对象
    • 使用 b前缀定义
      • 只允许 基本的 ASCII 使用字符形式 b"abc9"
      • 使用十六进制表示 b"x41x61"
  • [x] bytes 操作

    和str类型类似,都是不可变类型,所以方法很多都一样,只不过bytes的方法,输入是bytes,输出也是 bytes

    >>> b"abc".find(b"b")
    ... 1
    >>> b"abcdef".replace(b"f", b"123")
    ... b'abcde123'
    
    • [x] 类方法 bytes.fromhex(string) ---> bytes

      • string 必须是 2 个字符的 16进制的形式, "6162 6a 6b",空格将被忽略
      >>> bytes.fromhex("6162 6a 6b")
      ... b'abjk'
      >>> bytes.fromhex("6162 6a 6b").decode()
      ... 'abjk'
      >>> "中".encode()
      ... b'xe4xb8xad'
      >>> bytes.fromhex("e4b8ad")
      ... b'xe4xb8xad'
      >>> bytes.fromhex("e4b8ad").decode()
      ... '中'
      >>> "{:X}{:X}{:X}".format(*"中".encode())
      ... 'E4B8AD'
      >>> bytes.fromhex("{:X}{:X}{:X}".format(*"中".encode()))
      ... b'xe4xb8xad'
      >>> bytes.fromhex("{:X}{:X}{:X}".format(*"中".encode())).decode()
      ... '中'
      
    • [x] hex() ---> int

    • 返回 16 进制表示的字符串

      >>> "abc".encode().hex()
      ... '616263'
      >>> "中".encode().hex()
      ... 'e4b8ad'
      >>> bytes.fromhex("中".encode().hex())
      ... b'xe4xb8xad'
      >>> bytes.fromhex("中".encode().hex()).decode()
      ... '中'
      

  • [x] bytearray 定义

    • bytearray()bytearray
    • bytearray(int) 指定字节的bytearray,被 0 填充
    • bytearray(iterable_of_in) --> bytearray [0, 255] 的int组成的可迭代对象
    • bytearray(string, encoding[, errors]) -> bytearray 近似于 string.encode(),不过返回可变的对象
    • bytearray(bytes_or_buffer) -> immutable copy of bytes_or_buffer 从一个字节序列或者buffer复制除一个新的不可变的bytearray对象

    注意: b 前缀定义的类型是 bytes类型的

  • [x] bytearray 操作

    和 bytes 类型的方法相同

    >>> bytearray(b"abc").find(b"b")
    ... 1
    >>> bytearray(b"abcdef").replace(b"f", b"123")
    ... bytearray(b'abcde123')
    
    • [x] 类方法 bytearray.fromhex(string) ---> bytearray

      • string 必须是 2 个字符的 16进制的形式, "6162 6a 6b",空格将被忽略
      >>> bytearray.fromhex("6162 6a 6b")
      ... bytearray(b'abjk')
      >>> bytearray.fromhex("6162 6a 6b").decode()
      ... 'abjk'
      >>> "中".encode()
      ... b'xe4xb8xad'
      >>> bytearray.fromhex("e4b8ad")
      ... bytearray(b'xe4xb8xad')
      >>> bytearray.fromhex("e4b8ad").decode()
      ... '中'
      >>> "{:X}{:X}{:X}".format(*"中".encode())
      ... 'E4B8AD'
      >>> bytearray.fromhex("{:X}{:X}{:X}".format(*"中".encode()))
      ... bytearray(b'xe4xb8xad')
      >>> bytearray.fromhex("{:X}{:X}{:X}".format(*"中".encode())).decode()
      ... '中'
      
    • [x] hex() ---> int

    • 返回 16 进制表示的字符串

    • [x] 索引 ---> int
      bytearray(b"abcdef")[2] 返回改字节对应的数,int 类型

      >>> bytearray(b"abcdef")[2]
      ... 99
      

    • [x] append(int) 尾部追加一个元素
    • [x] insert(index,int) 在指定索引位置上插入元素
    • [x] extend(iterable_int) 将一个可迭代的整数集合追加到当前的 bytearry
    • [x] pop(index=-1) 从指定索引上移除元素,默认从尾部移除
    • [x] remove(value) 找到一个value移除,找不到 则 ValueError 异常

    上述方法中若要使用int类型,值必须在 [0, 255] 之间

    • [x] clear() 清空 bytearry
    • [x] reverse() 翻转 bytearry ,就地修改
原文地址:https://www.cnblogs.com/jingru-QAQ/p/11405626.html