SIngle character y and Y get dumped as YAML 1.1 booleans

余生长醉 提交于 2020-05-17 02:57:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!