在https://pypi.python.org/pypi/ruamel.yaml更改日志中有一个条目: 0.11.12(2016-07-06):
- added support for roundtrip of single/double quoted scalars using:
ruamel.yaml.round_trip_load(stream, preserve_quotes=True)
使用ruamel我可以转换:
skip: Skip
show: 'Show'
remove: "Remove"
"info_on": "ON"
为:
skip: Skip
show: Show
remove: Remove
info_on: ON
ruamel中是否有选项添加而不是保留引号:
"skip": "Skip"
"show": "Show"
"remove": "Remove"
"info_on": "ON"
答案 0 :(得分:1)
您可以使用dump()
选项default_style='"'
:
import sys
import ruamel.yaml
yaml_str = """\
skip: Skip
show: 'Show'
remove: "Remove"
"info_on": "ON"
"""
data = ruamel.yaml.round_trip_load(yaml_str)
ruamel.yaml.round_trip_dump(data, sys.stdout, default_style='"')
与您可以从中导出ruamel.yaml的旧PyYAML一样。