【发布时间】:2017-06-29 13:40:43
【问题描述】:
我需要为 YAML 文件中的现有键添加一个额外的值。以下是我正在使用的代码。
with open(yaml_in_path, 'r') as f:
doc, ind, bsi = load_yaml_guess_indent(f, preserve_quotes=True)
doc['phase1'] += ['c']
with open(yaml_out_path, 'w') as f:
ruamel.yaml.round_trip_dump(doc, f,
indent=2, block_seq_indent=bsi)
这是输入和输出。
输入
phase1:
- a
# a comment.
- b
phase2:
- d
输出
phase1:
- a
# a comment.
- b
- c
phase2:
- d
如何摆脱b 和c 之间的新行? (当phase1 是文件中的唯一键或phase1 和phase2 之间没有空行时,不会出现此问题。)
【问题讨论】:
标签: python yaml ruamel.yaml