如何阅读 Fortran 堡垒。# 其中 # 是 python 中的数字文件

How to read a fortran fort.# where # is a number file in python

我有一个 Fortran 代码的输出,如下所示:

结果存储在 fort.# 中,其中 # 是一般文件中的数字。如何在 python 中加载此文件,其中每一列都是不同的数组或数据框的一列?

我正在使用但不起作用的代码:

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

我也试过:

data  = np.fromfile(file_location,dtype = np.float32)
new_data = data.reshape() # something here but I can't read the shape

您可以在此处找到该文件: https://drive.google.com/drive/folders/1ohC2sHxs2M211HxIHd7yH2_Y5l5c1pQA?usp=sharing

假设你的文件都是相同的格式并且你不想要 header 然后下面读取你的文件并将它放入一个二维 numpy 数组,

import numpy as np    

my_data = []
with open('fort.21') as fort:
    
    for idx, row in enumerate(fort):
        
        if idx > 0:
            row_split = row.split('  ')[1:]
            temp_list = [float(r_s) for r_s in row_split]
            my_data.append(temp_list)
            
my_data = np.asarray(my_data)