问题
When I do:
from ruamel import yaml
seq = ["x", "y", "z", "Y", "true", True]
print(yaml.dump(seq, version=(1,1)))
it gives:
%YAML 1.1
--- [x, y, z, Y, 'true', true]
but I expected the y
and Y
to be quoted, because these get loaded back as booleans because this is YAML 1.1.
Moreover this
bug, indicates this problem is
solved.
Why is this bug marked as closed, when it still shows this error even on version ruamel.yaml>=0.15.93?
回答1:
You are using the unsafe PyYAML compatibility function dump()
(and besides you do so in an inefficient way). That function is outdated but emulates PyYAML's erroneous behaviour.
You
should instead
instantiating a YAML()
instance and using its .dump()
method.
import sys
import yaml as pyyaml
import ruamel.yaml
seq = ["x", "y", "z", "Y", "true", True]
print("PyYAML version:", pyyaml.__version__)
pyyaml.dump(seq, sys.stdout, default_flow_style=None, explicit_start=True, version=(1,1))
print()
yaml = ruamel.yaml.YAML(typ='safe')
yaml.version = (1,1)
yaml.default_flow_style=None
print("ruamel.yaml version:", ruamel.yaml.__version__)
yaml.dump(seq, sys.stdout)
which gives:
PyYAML version: 5.3.1
%YAML 1.1
--- [x, y, z, Y, 'true', true]
ruamel.yaml version: 0.16.10
%YAML 1.1
--- [x, 'y', z, 'Y', 'true', true]
来源:https://stackoverflow.com/questions/61252179/single-character-y-and-y-get-dumped-as-yaml-1-1-booleans