Skip to content
Advertisement

Tag: pyyaml

Pyyaml dump does not produce anchors for the same objects

I was experimenting a bit with PyYaml and I wanted to have a reference to a value appearing previously in the yaml. To give an example: from what I understood from the specifications pyyaml should be adding an anchor to each object that has already been encountered. In my case, I would expect to have in the yaml file: as

yaml anchors definitions loading in PyYAML

I’m using PyYAML. Is there a way to define a YAML anchor in a way it won’t be a part of the data structure loaded by yaml.load (I can remove “wifi_parm” from the dictionary but looking for a smarter way)? example.yaml: load_example.py: prints: I need: Answer The anchor information in PyYAML is discarded before you get the result from yaml.load().

A single string in single quotes with PyYAML

When I edit a YAML file in Python with PyYAML, all of my string values are saved back to the original file without quotes. I wanted one of those strings to be surrounded with single quotes: Changing the default_style parameter in yaml_dump affects whole file, which is not desired. I thought about adding single quotes to the beginning and end

How can I control what scalar form PyYAML uses for my data?

I’ve got an object with a short string attribute, and a long multi-line string attribute. I want to write the short string as a YAML quoted scalar, and the multi-line string as a literal scalar: I’d like the YAML to look like this: How can I instruct PyYAML to do this? If I call yaml.dump(my_obj), it produces a dict-like output:

Advertisement