加载 YAML 文件后,它不再是“yaml”;它现在是一个 Python 数据结构,biotools
键的内容是list
:
>>> import ruamel.yaml as yaml
>>> data = yaml.load(open('data.yml'))
>>> data['extra']['identifiers']['biotools']
['http://bio.tools/abyss']
像任何其他 Python 列表一样,您可以append
:
>>> data['extra']['identifiers']['biotools'].append('http://bio.tools/anothertool')
>>> data['extra']['identifiers']['biotools']
['http://bio.tools/abyss', 'http://bio.tools/anothertool']
如果你打印出数据结构,你会得到有效的 YAML:
>>> print( yaml.dump(data))
extra:
identifiers:
biotools: [http://bio.tools/abyss, http://bio.tools/anothertool]
当然,如果由于某种原因你不喜欢那个列表表示,你也可以得到语法上等价的:
>>> print( yaml.dump(data, default_flow_style=False))
extra:
identifiers:
biotools:
- http://bio.tools/abyss
- http://bio.tools/anothertool