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"\x41\x61"
  • [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'\xe4\xb8\xad'
      >>> bytes.fromhex("e4b8ad")
      ... b'\xe4\xb8\xad'
      >>> bytes.fromhex("e4b8ad").decode()
      ... '中'
      >>> "{:X}{:X}{:X}".format(*"中".encode())
      ... 'E4B8AD'
      >>> bytes.fromhex("{:X}{:X}{:X}".format(*"中".encode()))
      ... b'\xe4\xb8\xad'
      >>> bytes.fromhex("{:X}{:X}{:X}".format(*"中".encode())).decode()
      ... '中'
    • [x] hex() ---> int

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

      >>> "abc".encode().hex()
      ... '616263'
      >>> "中".encode().hex()
      ... 'e4b8ad'
      >>> bytes.fromhex("中".encode().hex())
      ... b'\xe4\xb8\xad'
      >>> 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'\xe4\xb8\xad'
      >>> bytearray.fromhex("e4b8ad")
      ... bytearray(b'\xe4\xb8\xad')
      >>> bytearray.fromhex("e4b8ad").decode()
      ... '中'
      >>> "{:X}{:X}{:X}".format(*"中".encode())
      ... 'E4B8AD'
      >>> bytearray.fromhex("{:X}{:X}{:X}".format(*"中".encode()))
      ... bytearray(b'\xe4\xb8\xad')
      >>> 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

bytes和bytearray相关推荐

  1. python bytearray拼接_python数据类型 ——bytes 和 bytearray

    bytes和 bytearray bytes:可以看作是一组二进制数值(0-255) 的 str 序列 bytearray:可以看作是一组二进制数值(0-255) 的 list 序列 bytes类型 ...

  2. TypeError: the JSON object must be str, bytes or bytearray, not dict

    TypeError: the JSON object must be str, bytes or bytearray, not dict 目录 TypeError: the JSON object m ...

  3. python3 bytes和bytearray总结

    The core built-in types for manipulating binary data are bytes and bytearray. They are supported by ...

  4. Python3: 字节类型 bytes 和 bytearray

    本文链接: https://blog.csdn.net/xietansheng/article/details/115557553 Python3 学习笔记(目录) 1. bytes Python 官 ...

  5. python中byte2array报错_python基础-bytes和bytearray的用法

    Python中的序列类型有bytes和bytearray. 二进制序列类型的用法比较少见,是python中少用的一种序列类型,对于二进制序列类型,大家基本了解即可. bytes二进制序列类型 指定长度 ...

  6. python中byte类型_详解python string类型 bytes类型 bytearray类型

    搜索热词 一.python3对文本和二进制数据做了区分.文本是Unicode编码,str类型,用于显示.二进制类型是bytes类型,用于存储和传输.bytes是byte的序列,而str是unicode ...

  7. Python 数据类型 bytes 与 bytearray 使用教程

    bytes 与 bytearray 是python非常重要的数据类型,但其重要性经常被我们忽视了.在实际开发过程中,又总是遇到 bytes 类型.举例,pickle 序列化, json序列化就是将对象 ...

  8. Python: bytes、bytearray 详解

    文章目录 写在开头的话 Python: bytes.bytearray 00. 概述 01.bytes定义 02. bytes操作 03. bytearray定义 04. bytearray操作 05 ...

  9. Python:bytes、bytearray

    bytes.bytearray Python3引入两个新类型 bytes:不可变字节序列 bytearray:字节数组.可变 字符串与bytes 字符串是字符组成的有序序列,字符可以使用编码来理解 b ...

最新文章

  1. 盘点:弱电施工过程中的电线电缆36计
  2. Python 5种方法实现单例模式
  3. 【数据迁移】使用传输表空间迁移数据
  4. boost::hana::symmetric_difference用法的测试程序
  5. ITK:将2D图像堆叠为3D图像
  6. FireBug命令行命令介绍
  7. 4.28—013—周日
  8. 计算机网络读书笔记(1)
  9. 游戏日志分析2:全方位数据采集
  10. linux socket句柄泄露,socket句柄泄漏问题的定位: losf和strace的联合使用!
  11. CentOs 开启ssh服务
  12. 酷似js的java函数简写——lambda表达式
  13. Excel批量打开URL
  14. Detours内联HOOK
  15. 微信小程序vtabs
  16. javaweb项目大概轮廓
  17. IDEA 在debug 模式下启动tomcat报错:Application Server was not ..reason:Unable to ping server at localhos:1199
  18. Hides for Mac(隐藏程序Mac老板键)
  19. 牛客网之黑暗的字符串
  20. Vue - 路由导航守卫控制访问权限,设置 localStorage 过期时间

热门文章

  1. 一位华为IT总监 职场是学习和感恩的
  2. 枚举BIG5中的汉字
  3. app模式会被第三方平台模式取代吗_甚至比专业软件更好用:那些被Win10取代的软件们...
  4. 怎么使用讯捷CAD编辑器编辑文字
  5. C++左值、右值、左值引用、右值引用的详解
  6. 如何在https网页发送http请求
  7. 单片机驱动LCD完整代码
  8. Unity人工智能AI编程知识
  9. 腾讯又刷屏了,升级员工关怀方案,员工法定退休可享荣誉金等福利
  10. Schematic and PCB(二)