如何解决如何读取fortran fort.#其中#是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
解决方法
假设您的文件都是相同的格式并且您不想要标题,那么以下内容将读取您的文件并将其放入一个 2D 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)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。