【问题标题】:Python 3.6 - Splitting hex dataPython 3.6 - 拆分十六进制数据
【发布时间】:2019-02-14 20:20:54
【问题描述】:

我正在尝试读取一个二进制文件并获取一个 utf-8 格式的标头。但是文件的其余部分具有超过十进制 127 的字节值,因此我无法将其转换为字符串。我必须拆分文本直到; (或 0x3B),我无法让它工作。

with open("test_qifs_single_frame.qifs", "rb") as file:
    data = file.read()

print(binascii.hexlify(data))

我也无法将其作为字符串读取,因为它告诉我无法将 0x81 解码为 UTF-8。据我了解,它超出了 ASCII 范围。我该怎么做才能解决这个问题?

【问题讨论】:

    标签: python-3.x split binary hex


    【解决方案1】:

    您可以逐字节读取文件,直到到达停止字符,然后对已读取的数据进行解码。

    创建一些示例数据

    >>> from random import randint
    >>> header = 'Heaðer;'.encode('utf-8')
    >>> bs = b''.join(bytes.fromhex('{:0>2x}'.format(randint(0, 255))) for _ in range(56))
    >>> with open('test_qifs_single_frame.qifs', 'wb') as f:
    ...     f.write(header + bs)
    >>>
    

    从文件中读取标题

    >>> # Create a bytearray to hold the bytes that we read.
    >>> ba = bytearray()
    
    >>> import functools
    >>> with open('test_qifs_single_frame.qifs', 'rb') as f:
    ...     breader = functools.partial(f.read, 1)
    ...     for b in iter(breader, b';'):
    ...         ba += b
    ... 
    >>> ba
    bytearray(b'Hea\xc3\xb0er')
    >>> ba.decode('utf-8')
    'Heaðer'
    

    如果iter 内置函数被传递了一个callable 和一个值,它将调用这个callable,直到它返回该值。在代码中,我们使用functools.partial 创建一个函数,该函数一次读取一个字节的文件,然后将其传递给iter

    【讨论】:

      猜你喜欢
      • 2015-07-19
      • 1970-01-01
      • 2019-03-27
      • 1970-01-01
      • 1970-01-01
      • 2013-01-18
      • 1970-01-01
      • 2014-02-18
      • 1970-01-01
      相关资源
      最近更新 更多