Reading YAML config file in python and using variables

后端 未结 2 513
栀梦
栀梦 2020-12-11 03:14

Say I have a yaml config file such as:

test1:
    minVolt: -1
    maxVolt: 1
test2:
    curr: 5
    volt: 5

I can read the file into python

2条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-11 03:50

    See Munch, Load YAML as nested objects instead of dictionary in Python

    import yaml
    from munch import munchify
    c = munchify(f)yaml.safe_load(…))
    print(c.test1.minVolt)
    # -1
    # Or
    f = open(…)
    c = Munch.fromYAML(f)
    

提交回复
热议问题