用python读取二进制文件

Reading a binary file with python(用python读取二进制文件)

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

问题描述

我发现用 Python 读取二进制文件特别困难.你能帮我个忙吗?我需要阅读这个文件,它在 Fortran 90 中很容易被阅读

I find particularly difficult reading binary file with Python. Can you give me a hand? I need to read this file, which in Fortran 90 is easily read by

int*4 n_particles, n_groups
real*4 group_id(n_particles)
read (*) n_particles, n_groups
read (*) (group_id(j),j=1,n_particles)

具体来说,文件格式是:

In detail, the file format is:

Bytes 1-4 -- The integer 8.
Bytes 5-8 -- The number of particles, N.
Bytes 9-12 -- The number of groups.
Bytes 13-16 -- The integer 8.
Bytes 17-20 -- The integer 4*N.
Next many bytes -- The group ID numbers for all the particles.
Last 4 bytes -- The integer 4*N. 

如何使用 Python 阅读此内容?我尝试了一切,但从未奏效.我有没有机会在 python 中使用 f90 程序,读取这个二进制文件,然后保存我需要使用的数据?

How can I read this with Python? I tried everything but it never worked. Is there any chance I might use a f90 program in python, reading this binary file and then save the data that I need to use?

推荐答案

读取二进制文件内容如下:

Read the binary file content like this:

with open(fileName, mode='rb') as file: # b is important -> binary
    fileContent = file.read()

然后使用struct.unpack解压"二进制数据:

then "unpack" binary data using struct.unpack:

起始字节:struct.unpack("iiiiii", fileContent[:20])

正文:忽略标题字节和尾随字节(= 24);剩下的部分构成正文,要知道正文中的字节数,进行整数除以 4;得到的商乘以字符串 'i' 为 unpack 方法创建正确的格式:

The body: ignore the heading bytes and the trailing byte (= 24); The remaining part forms the body, to know the number of bytes in the body do an integer division by 4; The obtained quotient is multiplied by the string 'i' to create the correct format for the unpack method:

struct.unpack("i" * ((len(fileContent) -24) // 4), fileContent[20:-4])

结束字节:struct.unpack("i", fileContent[-4:])

这篇关于用python读取二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:用python读取二进制文件

基础教程推荐