【发布时间】:2019-08-22 06:51:04
【问题描述】:
我正在解析 YAML 并将其分解为多个不同的 YAML 文件。我使用PyYAML的构造函数来实现,但是效果很差。
这是我项目的一部分,我需要根据收到的 yaml 文件中某个键的值来解析并拆分为多个不同的 yaml 文件。
我收到的 yaml 文件如下所示
testname: testname
testall:
test1:
name: name1
location: 0
test2:
name: name2
location: 2
test3:
name: name3
location: 0
test4:
name: name4
location: 2
...
locations:
- 0
- 2
- ...
我想解析它并按设备拆分,如下所示:
# location0.yaml
testname:test
tests:
test1:
name:test1
location:0
test3:
name: test3
location: 0
# location2.yaml
testname:test
tests:
test2:
name:test2
location:0
test4:
name: test4
location: 0
如上表格如何解析?
【问题讨论】:
标签: python-3.x yaml pyyaml ruamel.yaml