如何解决用python读取二进制文件
with open(fileName, mode='rb') as file: # b is important -> binary
fileContent = file.read()
然后使用struct.unpack “解压缩”二进制数据:
起始字节: struct.unpack("iiiii", fileContent[:20])
正文:忽略标题字节和尾随字节(=
24);剩下的部分构成主体,要知道主体中的字节数做一个整数除以4;将获得的商乘以字符串'i'
以为unpack方法创建正确的格式:
struct.unpack("i" * ((len(fileContent) -24) // 4), fileContent[20:-4])
结束字节:struct.unpack("i", fileContent[-4:])
解决方法
我发现用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程序,读取此二进制文件,然后保存需要使用的数据?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。