【问题标题】:Python3 convert bytes to an array containing escape charactersPython3 将字节转换为包含转义字符的数组
【发布时间】:2016-11-19 13:11:00
【问题描述】:

这最好用于 Python3,但如果这只能在 Python2 上工作,那也很好。

我想知道是否有一种方法可以从python中的字符串中提取转义字符,所以我将展示一个示例。

>>> bytes = open('pic.jpg','rb')
>>> bytes.read()
b'\\x00\xff\xed\x00XPhotoshop 3.0\x008BIM\x04\'
>>> bytesArray = bytes.split(escape_characters) # looking for a method to use here
>>> bytesArray
['\x00','\xff',etc]

【问题讨论】:

  • @furas 什么? OP 使用 bytes 作为其文件句柄的名称,隐藏了内置的 bytes 类型。
  • 真正想做什么?您想要一个包含 JPEG 文件中所有非可打印 ASCII 字符的字节的列表吗?
  • @PM2Ring 是的,就是这样
  • 这个 Python 3 代码应该可以帮助您入门:data = open('pic.jpg','rb').read(); a = [chr(u) for u in data if not 32 < u < 127]。如果您不想在a 中使用空格字符,请将 32 更改为 31。

标签: python arrays image


【解决方案1】:

也许你正在寻找的是这样的东西。

bytesArray = b'\x00\xff\xed\x00XPhotoshop 3.0\x008BIM\x04'

def splitBytes(bytesArray):
   return [hex(i) for i in bytesArray]

输出

['0x0', '0xff', '0xed', '0x0', '0x58', '0x50', '0x68', '0x6f', '0x74', '0x6f', '0x73', '0x68', '0x6f', '0x70', '0x20', '0x33', '0x2e', '0x30', '0x0', '0x38', '0x42', '0x49', '0x4d', '0x4']

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-04
    • 2019-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多