就个人而言,我会使用bitstring模块,但在编写时我可能会有偏见.作为示例,在手册中有一些简单的读/写二进制格式代码.
这是通过二进制格式创建的一种方法:
fmt = 'sequence_header_code,
uint:12=horizontal_size_value,
uint:12=vertical_size_value,
uint:4=aspect_ratio_information,
...
'
d = {'sequence_header_code': '0x000001b3',
'horizontal_size_value': 352,
'vertical_size_value': 288,
'aspect_ratio_information': 1,
...
}
s = bitstring.pack(fmt, **d)
Run Code Online (Sandbox Code Playgroud)
以及之后解析它的一种方法:
>>> s.unpack('bytes:4, 2*uint:12, uint:4')
['\x00\x00\x01\xb3', 352, 288, 1]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
943 次 |
| 最近记录: |