我发现使用Python读取二进制文件特别困难。你能帮我一下吗? 我需要读取这个文件,而在Fortran 90中很容易读取它。
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)
具体而言,文件格式如下:
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程序读取这个二进制文件并保存我需要使用的数据?
fromfile
函数使读取二进制文件变得轻而易举。我建议使用它。 - littleO