【问题标题】:Huge SETTING file to a dictionary in Python巨大的 SETTING 文件到 Python 中的字典
【发布时间】:2017-10-18 06:57:30
【问题描述】:

我有一个大的 .SET 文件,它提供了来自第三方软件的边界和求解器设置。该文件根据仿真时给定的设置更改其参数的大小和位置。它有100个参数。

典型的设置文件如下所示:

(rp (
(wall-film/model-parameters ((solve-wallfilm? . #t) (solve-momentum? . #f) 
(solve-energy? . #t) (solve-scalar? . #f) (solve-vapor? . 2) (dpm-
collection? . #f) (mom-gravity? . #t) (mom-aero-drive? . #t) (mom-wall-visc? 
. #t) (mom-pressure? . #f) (mom-spreading? . #f) (ewf-adaptive? . #f) (time-
scheme . 0) (mass-scheme . 0) (mom-scheme . 0) (energy-scheme . 0) (scalar-
scheme . 0) (reconstruct-limiter . 0) (thickness-limit . 1e-05) (thickness-
realistic . 0.) (courant-number . 0.2) (adapt-init-dt . 0.0001) (timestep-
max . 0.01) (sub-time-steps . 10) (sub-iter-nums . 10)) 

我想创建一个函数来读取文件并为所有参数创建如下字典:

 A = {'solve-wallfilm':'t','solve momentum':'t','solve-energy':'t'}

这将有助于我将来比较两个不同的设置文件。

有人可以建议我如何在 python 中做到这一点,或者我应该使用什么库来将设置文件以有序的格式放置?

提前致谢

【问题讨论】:

    标签: python file dictionary settings


    【解决方案1】:

    有点像

    def settingtodict(filepath):
        d = {}
        with open(filepath,'r') as f:
            for line in f:
                a = line.split("(")
                for each in a:
                    print(each)
                    if '.' in each:
                        d[each.split('.')[0]]=each.split('.')[1][1:-2]
        return d
    

    只要一切都在一条线上就可以工作

    【讨论】:

      猜你喜欢
      • 2014-03-02
      • 2011-01-07
      • 2015-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多