SIngle字符y和Y作为YAML 1.1布尔值转储 [英] SIngle character y and Y get dumped as YAML 1.1 booleans
问题描述
当我这样做时:
from ruamel import yaml
seq = ["x", "y", "z", "Y", "true", True]
print(yaml.dump(seq, version=(1,1)))
它给出:
%YAML 1.1
--- [x, y, z, Y, 'true', true]
,但我希望引用y
和Y
,因为它们是布尔值,因为这是YAML 1.1,因此被重新加载为布尔值.
而且此
错误,表明此问题是
解决.
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.
为什么即使在ruamel.yaml> = 0.15.93版本上仍显示此错误,此错误仍标记为已关闭?
Why is this bug marked as closed, when it still shows this error even on version ruamel.yaml>=0.15.93?
推荐答案
您正在使用不安全的PyYAML兼容性函数dump()
(此外,这样做的效率很低).该功能已过时,但模仿了PyYAML的错误行为.
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.
您
应该改为
实例化YAML()
实例并使用其.dump()
方法.
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)
给出:
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]
这篇关于SIngle字符y和Y作为YAML 1.1布尔值转储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!