【发布时间】:2016-06-29 00:10:47
【问题描述】:
我有一个 python 有序字典,例如,
from collections import OrderedDict
a = OrderedDict()
a['name'] = 'hello'
a['msgs'] = ['hello', 'world']
我将其转换为 YAML 语法,
import yaml
with open("b.yaml", 'w') as stream:
stream.write(yaml.dump(a))
打印出来,
!!python/object/apply:collections.OrderedDict
- - [name, hello]
- - msgs
- [hello, world]
然而,我希望 YAML 格式更简单,
name : hello
msgs:
- hello
- world
如何强制 YAML 使用 hypen + space 表示法而不是像 [a,b,c,d] 表示法那样的 JSON 打印列表项?
为什么 PyYAML 将 Ordered dict 项打印为 [name, hello] 而不是 name : hello?
【问题讨论】: