Reputation: 703
Already read https://stackoverflow.com/a/61252180/1676006 and it doesn't seem to have solved my problem.
Using ruamel.yaml:
yaml = YAML(typ="safe")
yaml.version = (1, 1)
yaml.default_flow_style = None
yaml.dump(stuff, sys.stdout)
With a python dict stuff
containing:
{
"key": "Y"
}
outputs
%YAML 1.1
--- {key: Y}
in the output yaml. In my actual code, I'm piping this into helm, which is backward compatible with yaml 1.1, meaning that I need to have anything that might be a boolean scalar quoted. What am I missing here?
E: because I am a big dummy and forgot to update everything after trying a min repro - here's the REPL output:
>>> from ruamel.yaml import YAML
>>> yaml = YAML()
>>> yaml.version = (1,1)
>>> yaml.default_flow_style = None
>>> stuff = {
... "key": "T"
... }
>>> import sys
>>> yaml.dump(stuff, sys.stdout)
%YAML 1.1
--- {key: T}
>>>
E again: I am shamed. T isn't a scalar boolean, I was using the wrong test subject. This is not a place of honour.
Upvotes: 0
Views: 175
Reputation: 76902
If your Python dict really consists of a single key value pair, mapping of a string to a string, as you indicate, ruamel.yaml
will not dump the output you display
with or without typ='safe'
)
import sys
import ruamel.yaml
data = {
"key": "Y"
}
yaml = ruamel.yaml.YAML(typ='safe')
yaml.version = (1, 1)
yaml.default_flow_style = None
yaml.dump(data, sys.stdout)
yaml = ruamel.yaml.YAML()
yaml.version = (1, 1)
yaml.default_flow_style = None
yaml.dump(data, sys.stdout)
which gives:
%YAML 1.1
--- {key: 'Y'}
%YAML 1.1
--- {key: 'Y'}
Not even changing the value to a boolean gets your result
import sys
import ruamel.yaml
data = dict(key=True)
yaml = ruamel.yaml.YAML(typ='safe')
yaml.version = (1, 1)
yaml.default_flow_style = None
yaml.dump(data, sys.stdout)
yaml = ruamel.yaml.YAML()
yaml.version = (1, 1)
yaml.default_flow_style = None
yaml.dump(data, sys.stdout)
which gives:
%YAML 1.1
--- {key: true}
%YAML 1.1
--- {key: true}
Always, always, always provide a minimal program that can be cut-and-pasted to get the results that you are seeing.
That is how I produce my answers ( the answers are actually comming from a multidocument YAML file processed by (ryd
)[https://pypi.org/project/ryd/)), and that is how you should consider providing your questions.
Upvotes: 2