使用 struct.pack 和 struct.unpack_from 写入数据结构读取不正确 [英] Write data structure not read correct with struct.pack and struct.unpack_from

查看:199
本文介绍了使用 struct.pack 和 struct.unpack_from 写入数据结构读取不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在二进制文件中获取数据时遇到这个问题

#写入数据f = 打开(路径,'wb')开始日期 = [2014, 1, 1, 0, 0, 0, 0]end_date = [2014, 2, 1, 0, 0, 0, 0]对于范围内的 x(10):f.write(struct.pack('B', 0))f.write(struct.pack('I', x))f.write(struct.pack('HBBBBBH', *start_date_binary))f.write(struct.pack('HBBBBBH', *end_date_binary))f.close()# 读取数据f = 打开(路径,'rb')对于范围内的 x(10):data_structure = struct.unpack_from("BIHBBBBBHHBBBBBH",f.read(FILE_INDEX_STRUCTURE))打印(数据结构)f.close()

输出是

(0, 0, 2014, 1, 1, 0, 0, 0, 0, 56832, 7, 2, 1, 0, 0, 0)(0, 17292800, 1, 0, 0, 0, 0, 0, 2014, 258, 0, 0, 0, 0, 0, 0)(7, 257, 0, 0, 0, 222, 7, 2, 1, 0, 0, 0, 0, 0, 3, 0)(0, 0, 56832, 7, 2, 1, 0, 0, 0, 0, 0, 4, 0, 0, 0, 2014)(0, 131989504, 258, 0, 0, 0, 0, 0, 0, 5, 0, 0, 222, 7, 1, 1)(222, 66055, 0, 0, 0, 0, 0, 6, 0, 56832, 7, 1, 1, 0, 0, 0)(1, 0, 0, 0, 7, 0, 0, 0, 2014, 257, 0, 0, 0, 0, 0, 56832)(0, 0, 8, 0, 0, 222, 7, 1, 1, 0, 0, 0, 0, 222, 7, 258)(0, 2304, 56832, 7, 1, 1, 0, 0, 0, 0, 222, 7, 2, 1, 0, 0)

预期的输出是

(0, 0, 2014, 1, 1, 0, 0, 0, 0, 2014, 2, 1, 0, 0, 0, 0)(0, 1, 2014, 1, 1, 0, 0, 0, 0, 2014, 2, 1, 0, 0, 0, 0)(0, 2, 2014, 1, 1, 0, 0, 0, 0, 2014, 2, 1, 0, 0, 0, 0)(0, 3, 2014, 1, 1, 0, 0, 0, 0, 2014, 2, 1, 0, 0, 0, 0)(0, 4, 2014, 1, 1, 0, 0, 0, 0, 2014, 2, 1, 0, 0, 0, 0)(0, 5, 2014, 1, 1, 0, 0, 0, 0, 2014, 2, 1, 0, 0, 0, 0)(0, 6, 2014, 1, 1, 0, 0, 0, 0, 2014, 2, 1, 0, 0, 0, 0)(0, 7, 2014, 1, 1, 0, 0, 0, 0, 2014, 2, 1, 0, 0, 0, 0)(0, 8, 2014, 1, 1, 0, 0, 0, 0, 2014, 2, 1, 0, 0, 0, 0)(0, 9, 2014, 1, 1, 0, 0, 0, 0, 2014, 2, 1, 0, 0, 0, 0)

编辑

获取'B'为1且'H'为2的项目结构类型.在同一个unpack中使用这些类型函数类型混淆,在示例中 'BH' 为 3 但返回 4.

<预><代码>>>>struct.unpack_from("B", '')...struct.error: unpack_from 需要至少 1 个字节的缓冲区>>>struct.unpack_from("H", '')...struct.error: unpack_from 需要至少 2 个字节的缓冲区>>>struct.unpack_from("BH", '')...struct.error: unpack_from 需要至少 4 个字节的缓冲区

解决方案

您遇到了填充问题.正如文档所说:

<块引用>

填充仅在连续的结构成员之间自动添加.在编码结构的开头或结尾不添加填充.

看看发生了什么:

<预><代码>>>>struct.pack("B", 0)'\x00'>>>struct.pack("我", 1)'\x01\x00\x00\x00'>>>struct.pack("BI", 0, 1)'\x00\x00\x00\x00\x01\x00\x00\x00'

所以你不能单独包装物品,然后将它们一起拆开,除非你自己添加填充物...

<预><代码>>>>struct.pack("B0I", 0)'\x00\x00\x00\x00'

或完全关闭填充:

<预><代码>>>>struct.pack("=BI", 0, 1)'\x00\x01\x00\x00\x00'

I have this problem in get data in binary file

# Write data
f = open(path, 'wb')

start_date = [2014, 1, 1, 0, 0, 0, 0]
end_date = [2014, 2, 1, 0, 0, 0, 0]

for x in range(10):
    f.write(struct.pack('B', 0))
    f.write(struct.pack('I', x))
    f.write(struct.pack('HBBBBBH', *start_date_binary))
    f.write(struct.pack('HBBBBBH', *end_date_binary))

f.close()

# Read data
f = open(path, 'rb')
for x in range(10):
    data_structure = struct.unpack_from("BIHBBBBBHHBBBBBH",
                                        f.read(FILE_INDEX_STRUCTURE))
    print(data_structure)

f.close()

Output is

(0, 0, 2014, 1, 1, 0, 0, 0, 0, 56832, 7, 2, 1, 0, 0, 0)
(0, 17292800, 1, 0, 0, 0, 0, 0, 2014, 258, 0, 0, 0, 0, 0, 0)
(7, 257, 0, 0, 0, 222, 7, 2, 1, 0, 0, 0, 0, 0, 3, 0)
(0, 0, 56832, 7, 2, 1, 0, 0, 0, 0, 0, 4, 0, 0, 0, 2014)
(0, 131989504, 258, 0, 0, 0, 0, 0, 0, 5, 0, 0, 222, 7, 1, 1)
(222, 66055, 0, 0, 0, 0, 0, 6, 0, 56832, 7, 1, 1, 0, 0, 0)
(1, 0, 0, 0, 7, 0, 0, 0, 2014, 257, 0, 0, 0, 0, 0, 56832)
(0, 0, 8, 0, 0, 222, 7, 1, 1, 0, 0, 0, 0, 222, 7, 258)
(0, 2304, 56832, 7, 1, 1, 0, 0, 0, 0, 222, 7, 2, 1, 0, 0)  

Expected output is that

(0, 0, 2014, 1, 1, 0, 0, 0, 0, 2014, 2, 1, 0, 0, 0, 0)
(0, 1, 2014, 1, 1, 0, 0, 0, 0, 2014, 2, 1, 0, 0, 0, 0)
(0, 2, 2014, 1, 1, 0, 0, 0, 0, 2014, 2, 1, 0, 0, 0, 0)
(0, 3, 2014, 1, 1, 0, 0, 0, 0, 2014, 2, 1, 0, 0, 0, 0)
(0, 4, 2014, 1, 1, 0, 0, 0, 0, 2014, 2, 1, 0, 0, 0, 0)
(0, 5, 2014, 1, 1, 0, 0, 0, 0, 2014, 2, 1, 0, 0, 0, 0)
(0, 6, 2014, 1, 1, 0, 0, 0, 0, 2014, 2, 1, 0, 0, 0, 0)
(0, 7, 2014, 1, 1, 0, 0, 0, 0, 2014, 2, 1, 0, 0, 0, 0)
(0, 8, 2014, 1, 1, 0, 0, 0, 0, 2014, 2, 1, 0, 0, 0, 0)
(0, 9, 2014, 1, 1, 0, 0, 0, 0, 2014, 2, 1, 0, 0, 0, 0)

EDIT

Getting type of structure of item where 'B' is 1 and 'H' is 2. Using those types in the same unpack function the types are confused and in example where 'BH' is 3 but return 4.

>>> struct.unpack_from("B", '')
...
struct.error: unpack_from requires a buffer of at least 1 bytes
>>> struct.unpack_from("H", '')
...
struct.error: unpack_from requires a buffer of at least 2 bytes
>>> struct.unpack_from("BH", '')
...
struct.error: unpack_from requires a buffer of at least 4 bytes

解决方案

You're running into padding problems. As the docs say:

Padding is only automatically added between successive structure members. No padding is added at the beginning or the end of the encoded struct.

See what's happening:

>>> struct.pack("B", 0)
'\x00'
>>> struct.pack("I", 1)
'\x01\x00\x00\x00'
>>> struct.pack("BI", 0, 1)
'\x00\x00\x00\x00\x01\x00\x00\x00'

So you can't pack items separately and then unpack them together, unless you add the padding yourself...

>>> struct.pack("B0I", 0)
'\x00\x00\x00\x00'

or turn off padding altogether:

>>> struct.pack("=BI", 0, 1)
'\x00\x01\x00\x00\x00'

这篇关于使用 struct.pack 和 struct.unpack_from 写入数据结构读取不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆