我想从下面的Fortran代码读取二进制文件输出,但结果与输出文件不一样。Python读取Fortran二进制文件
Fortran 77的代码:
program test
implicit none
integer i,j,k,l
real*4 pcp(2,3,4)
open(10, file='pcp.bin', form='unformatted')
l = 0
do i=1,2
do j=1,2
do k=1,2
print*,k+l*2
pcp(i,j,k)=k+l*2
l = l + 1
enddo
enddo
enddo
do k=1,4
write(10)pcp(:,:,k)
enddo
close(10)
stop
end
我尝试使用下面的Python代码:
from scipy.io import FortranFile
f = FortranFile('pcp.bin', 'r')
a = f.read_reals(dtype=float)
print(a)
[读取在Python中直接访问fortran未格式化文件]可能重复(http://stackoverflow.com/questions/10475839/reading-a-direct-access-fortran-unformatted-file-in-python) –
可能复制[Python:阅读Fortran二进制文件使用numpy或scipy](http://stackoverflow.com/questions/30307305/python-reading-fortran-binary-file-using-numpy-or-scipy) – glls
谢谢你们,但我尝试两种解决方案。在我的Fortran代码中有一个3d变量和真正的* 4精度。 – marcelorodrigues