【发布时间】:2021-10-30 12:55:01
【问题描述】:
我有这个示例 YAML:
people:
name: abc
address: '55 Oxford Street, San Jose 95134. CA'
occupation: 'Travel Blogger'
Hobby: 'Travelling'
additional_interests: []
other_info: [['gender', 'male'], ['no_of_cars', 'three'],
['hair_color', 'black'], ['eye_color', 'brown']]
我想以这样的方式呈现它,输出 yaml 应该是:
people:
name: abc
address: '55 Oxford Street, San Jose 95134. CA'
occupation: 'Travel Blogger'
Hobby: 'Travelling'
additional_interests: []
other_info: [
['gender', 'male'],
['no_of_cars', 'three'],
['hair_color', 'black'],
['eye_color', 'brown']
]
我正在使用以下代码:
from ruamel.yaml import YAML as Ruamel
from pathlib import Path
yaml = Ruamel(typ='rt')
config_file = Path('/Users/test_config.yaml')
configs = yaml.load(config_file.read_text())
component_file="xyz.txt"
with open(component_file, 'w') as component:
for key, value in configs.items():
yaml.default_flow_style=None
yaml.dump({key: value}, component)
这里的想法是将现有的 YAML 文件转换为更易于阅读的格式。任何指针如何实现我正在寻找的格式 - 对于 YAML 中的嵌套列表?
【问题讨论】:
标签: python-3.x dictionary yaml ruamel.yaml