在Fortran中读取一个用Python中的tobytes保存的矩阵

时间:2017-06-22 13:54:22

标签: python numpy fortran binaryfiles

我用ndarray.tobytes(order='F')在Python中保存了一个矩阵。

f_mat = open('mat.dat', 'wb')
f_mat.write(matrix.tobytes('F'))
f_mat.close()

有没有快速的方法在Fortran中阅读它,还是我应该循环并找出元素的顺序?

1 个答案:

答案 0 :(得分:2)

如果你知道大小(n时间m)和数组的类型并相应地声明它:

!just an example
real(c_double), allocatable :: mat(:,:)
allocate(mat(n,m))

应该只是

open(newunit = u, file='mat.dat', access='stream', form='unformatted', status='old', action='read')
read(u) mat
close(u)

open之后access='stream'的参数是可选的。