将二进制文件读入结构 [英] Reading a binary file into a struct

查看:88
本文介绍了将二进制文件读入结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个格式/结构已知的二进制文件.

I have a binary file with a known format/structure.

如何将所有二进制数据读入结构的数组中?

How do I read all the binary data in to an array of the structure?

类似的东西(用伪代码)

Something like (in pseudo code)

bytes = read_file(filename)
struct = {'int','int','float','byte[255]'}
data = read_as_struct(bytes, struct)
data[1]
>>> 10,11,10.1,Arr[255]

到目前为止的解决方案:

Solution so far:

data = []

fmt   = '=iiiii256i'
fmt_s = '=iiiii'
fmt_spec = '256i'

struct_size = struct.calcsize(fmt)

for i in range(struct_size, len(bytes)-struct_size, struct_size):
    dat1= list(struct.unpack(fmt_s, bytes[i-struct_size:i-1024]))
    dat2= list(struct.unpack(fmt_spec, bytes[i-1024:i]))
    dat1.append(dat2)
    data.append(dat1)

推荐答案

使用 struct模块;您需要以该库记录的字符串格式定义类型:

Use the struct module; you need to define the types in a string format documented with that library:

struct.unpack('=HHf255s', bytes)

上面的示例期望本机字节顺序,两个无符号短裤,一个浮点数和一个255个字符的字符串.

The above example expects native byte-order, two unsigned shorts, a float and a string of 255 characters.

要遍历一个已经完全读取的bytes字符串,我将使用itertools;有一个方便的石斑鱼食谱我在这里有适配器:

To loop over an already fully read bytes string, I'd use itertools; there is a handy grouper recipe that I've adapter here:

from itertools import izip_longest, imap
from struct import unpack, calcsize

fmt_s = '=5i'
fmt_spec = '=256i'
size_s = calcsize(fmt_s)
size = size_s + calcsize(fmt_spec)

def chunked(iterable, n, fillvalue=''):
    args = [iter(iterable)] * n
    return imap(''.join, izip_longest(*args, fillvalue=fillvalue))

data = [unpack(fmt_s, section[:size_s]) + (unpack(fmt_spec, section[size_s:]),)
    for section in chunked(bytes, size)]

这会生成元组而不是列表,但是如果需要调整,它很容易调整

This produces tuples rather than lists, but it's easy enough to adjust if you have to:

data = [list(unpack(fmt_s, section[:size_s])) + [list(unpack(fmt_spec, section[size_s:]))]
    for section in chunked(bytes, size)]

这篇关于将二进制文件读入结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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