【发布时间】:2016-03-26 02:01:22
【问题描述】:
我需要存储一个包含 4 个字段的 12 字节标头的二进制文件。它们分别是:sSamples(4 字节整数)、sSampPeriod(4 字节整数)、sSampSize(2 字节整数),最后是 sParmKind(2 字节整数)。 我正在对所需字段的变量使用“结构”。现在我已经分别定义了它们,如何将它们全部合并以存储“12 字节标头”?
sSamples = struct.pack('i', nSamples) # 4-bytes integer
sSampPeriod = struct.pack('i', nSampPeriod) # 4-bytes integer
sSampSize = struct.pack('H', nSampSize) # 2-bytes integer / unsigned short
sParmKind = struct.pack('H', 9) # 2-bytes integer / unsigned short
此外,我还有一个维度为 D (numpy.ndarray - float32) 的 npVect float 数组。如何将此向量存储在同一个二进制文件中,但在标题之后?
【问题讨论】:
-
请注意,您可以一次打包多个值:struct.pack('Hi', foo, bar)。此外,您应该在格式字符串之前放置一个 '>' 或 '
标签: python header-files binaryfiles