use*_*r_h 4 python variables yaml
我有一个 YAML 文件,其内容如下:
a: 45
b: cheese
c: 7.8
Run Code Online (Sandbox Code Playgroud)
我希望能够将这些信息读取到 python 中的变量中,但是经过大量的谷歌搜索后,我无法确定这是否可能,或者我是否必须先将其放入另一个 python 类型中,然后才能将其放入变量中?最终我想使用 a 和 c 的值进行计算。
一般来说,使用字典并没有什么问题,因为使用会导致yaml.load("""a: 45\nb: cheese\nc: 7.8""")- 如果你确实必须将它们作为实际变量放入命名空间中,你可以这样做:
>>> y = yaml.load("""a: 45
... b: cheese
... c: 7.8""")
>>> globals().update(y)
>>> b
"cheese"
Run Code Online (Sandbox Code Playgroud)
但我不推荐它。