读取/修改fortran文件

时间:2017-09-25 16:09:54

标签: python regex find

我有一个大的fortran(f90)文件,用于生成数值模拟的输入文件。我想创建一个GUI来设置f90文件中编写的模拟参数。 现在我正在尝试创建一个python脚本,可以查找,存储和修改(如果需要)这些变量(有很多变量)。

我正在尝试访问以这种方式存储的值(这是一个缩短的版本):

! ib  : ply orientation
!       ply at   0 : ib = 1
!       ply at  90 : ib = 2
!       ply at  45 : ib = 3
!       ply at -45 : ib = 4
a = 7500.
b = 8.
e = 579.
!--------------Centre 
x0 = 0.
y0 = 0. 
!--------------Radius 
Rfoc = 1600.
!--------------End of geometric parameters

到目前为止,代码看起来像这样:

import re

geomVar = ["a =","b =","e =","Rfoc ="]

endGeom = "End of geometric parameters"

def goFetchGeom(fileNameVariable, valueInput):
    valueOutput = []

    for line in fileNameVariable.readlines():
        # Looking for svalue numbers associated with search value
        if any(string in line for string in valueInput):
            result = [float(s) for s in re.findall(r'\b\d+\b', line)]
            valueOutput.append(result[0])

        # End of search loop to avoid reading the whole file
        elif endGeom in line :
            fileNameVariable.seek(0) #rewind the file
            return valueOutput

with open("mesh_Comp_UD.f90", "r+") as f:
    geomValue = goFetchGeom(f,geomVar)

print geomValue

第一个问题:它找到的行有" ib = ..."因为变量" b ="存储在geomVar中。

第二个问题:如果一行有多个变量,我只得到第一个,而不是全部。

因为它们很多,我想直接在函数中传递我以数组的形式寻找的变量。此外,由于文件很长,我不想从头开始搜索每个变量。

0 个答案:

没有答案
相关问题