ARTICLE AD BOX
Is there a reason why yaml adds single quotation marks around the words yes and no when I create a YAML file from a python dict?
MWE:
import yaml def yes_or_no( word: str, ) -> dict: thestring = { "theword": word, } return thestring thedict = yes_or_no("yes") # Writing nested data to a YAML file with open("output.yaml", "w") as file: yaml.dump(thedict, file)This returns the following YAML file:
theword: 'yes'When I call yes_or_no("what"), the YAML file is
theword: whatwithout single quotation marks around the word what. How can I avoid the single quotation marks around the words yes and no?
1558 bronze badges
4
Use ruamel.yaml to change yaml to version 1.2, which does support this. Example:
from ruamel.yaml import YAML def yes_or_no( word: str, ) -> dict: thestring = { "theword": word, } return thestring thedict = yes_or_no("yes") yaml = YAML() yaml.version = (1, 2) with open("output.yaml", "w") as file: yaml.dump(thedict, file)Explore related questions
See similar questions with these tags.
