【发布时间】:2018-04-17 07:07:27
【问题描述】:
我需要编写函数来读取 YAML 文件并更新特定值。 YAML 文件是字典,
sample :
test_example:
parent:
attribute_1: 2
attribute_2: 2
parent2:
childList:
- group: 2
type: "test"
track_int:
- key_1: 3
key_2: 25
state: present
state: present
- group: 4
typr: "old"
track_int:
- key_1: 3
key_2: 25
state: present
state: present
现在我需要编写函数来传递密钥,它应该替换特定值的值
前 - 将 test_example["parent2"]["childList"][0]["group"] 更新为 4
并将test_example["parent"]["attribute_2"] 更新为5
我该怎么做?
【问题讨论】:
-
这个 YAML 文件/文档不是字典,它是映射和序列(和标量)的组合。在根目录有一个映射,它作为 Python 字典加载,但是说“YAML 文件是 [a] 字典”是不正确的。
标签: python yaml file-handling pyyaml