且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python:使用numpy或scipy读取Fortran二进制文件

更新时间:2021-09-16 01:48:49

np.fromfile takes a "count" argument, which specifies how many items to read. If you know the number of integers in the header in advance, a simple way to do what you want without any type conversions would just be to read the header as integers, followed by the rest of the file as floats:

with open('filepath','r') as f:
    header = np.fromfile(f, dtype=np.int, count=number_of_integers)
    data = np.fromfile(f, dtype=np.float32)