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: what

without single quotation marks around the word what. How can I avoid the single quotation marks around the words yes and no?

kalle's user avatar

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)

exploding-duck's user avatar

1 Comment

Thank you so much! ruamel.yaml did the trick!

2026-01-07T02:04:24.493Z+00:00

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.