简体   繁体   English

Python - 读取和编写结构化二进制文件

[英]Python - Reading and Writing Structured Binary Files

Currently I'm trying to manipulate a kind of binary file, which structure is like this: 目前我正在尝试操纵一种二进制文件,其结构如下:

FileHeader + [SectionHeader + Binary(1D-array)] * NumSetions

After searching on the internet, I came up with the following code to read it: 在互联网上搜索后,我想出了以下代码来阅读它:

import numpy as np

# Always BIG Endian
#file_header bytes: 12
file_header_dtype = np.dtype([
    ('nsection', '>i4'), # number of sections
    ('nsample', '>i4'), # number of samples for each section
    ('dt', '>f4'), # sampling rate
    ])

#section_header bytes: 28
section_header_dtype = np.dtype([
    ('rec', '>i4'), # record number
    ('x', '>f8'), # x, in meter
    ('y', '>f8'), # y, in meter
    ('z', '>f8'), # z, in meter
    ])

def dtype_section(nt):
    return np.dtype(section_header_dtype.descr + [('binary', ('>f4',nt))])

def read_file_header(_file):
    with open(_file, 'rb') as f:
        file_header = np.fromstring(f.read(12), dtype=file_header_dtype)
    return file_header

def readFile(filename):
    raw = open(filename, 'rb')
    file_header = np.fromstring(raw.read(12), dtype=file_header_dtype)
    nt = file_header['nsample'][0]
    dtype_file_sections = dtype_section(nt)
    data = np.fromstring(raw.read(), dtype=dtype_file_sections)
    return (file_header, data)

With this way, I can easily call the header-striped binary part and perform plt.imshow or anything else. 通过这种方式,我可以轻松调用标题条带二进制部分并执行plt.imshow或其他任何操作。

data = readFile('site1.bin')
data_arr = data[1]['binary']
#plt.imshow(data_arr)

The problem is, I cannot find a way to output the data while maintaining same data structure 问题是,我找不到在保持相同数据结构的同时输出数据的方法

ndarray.tofile() only works for one array per time ndarray.tofile()每次仅适用于一个数组

And np.array((data[0],data[1])).tofile('test') would cause IOError np.array((data[0],data[1])).tofile('test')会导致IOError

IOError: cannot write object arrays to a file in binary mode IOError:无法以二进制模式将对象数组写入文件

I'm pretty new to Python and I'm not sure if I made any mistake. 我对Python很陌生,我不确定我是否犯了任何错误。 Or should I consider another way to read this kind of file, rather than using numpy.dtype ? 或者我应该考虑另一种方式来阅读这种文件,而不是使用numpy.dtype Please help me. 请帮我。

The straightforward way would be to simply write to a binary file: 直接的方法是简单地写入二进制文件:

with open('test','wb') as f:
    f.write(data[0].tobytes())
    f.write(data[1].tobytes())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM