np.fromfile with count = -1会添加意外的零

时间:2017-06-02 13:56:32

标签: python python-2.7 fortran binaryfiles

我正在尝试使用np.fromfile来读取我使用直接访问使用Fortran编写的二进制文件。但是,如果我设置count=-1而不是max_items,则np.fromfile会返回比预期更大的数组;将0添加到我用二进制写的向量中。

Fortran测试代码:

program testing
implicit none
integer*4::i
open(1,access='DIRECT', recl=20, file='mytest', form='unformatted',convert='big_endian')
write(1,rec=1) (i,i=1,20)
close(1)
end program

我如何使用np.fromfile

import numpy as np
f=open('mytest','rb')
f.seek(0)
x=np.fromfile(f,'>i4',count=20)
print len(x),x

因此,如果我像这样使用它,则会返回我的[1,...,20] np数组,但设置count=-1会返回大小为1600的[1,...,20,0,0,0,0,0]

我正在使用一个小端机器(不应该影响任何东西),我正在使用ifort编译Fortran代码。

我很好奇这种情况发生的原因,以避免将来出现任何意外。

0 个答案:

没有答案