Reading a binary file with 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读取二进制文件
基础教程推荐
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- Kivy 使用 opencv.调整图像大小 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
- 在 Python 中将货币解析为数字 2022-01-01
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
