我想從下面的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