Skip to content
Advertisement

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:

JavaScript

I’d like the YAML to look like this:

JavaScript

How can I instruct PyYAML to do this? If I call yaml.dump(my_obj), it produces a dict-like output:

JavaScript

(Not sure why long is double-spaced like that…)

Can I dictate to PyYAML how to treat my attributes? I’d like to affect both the order and style.

Advertisement

Answer

Based on Any yaml libraries in Python that support dumping of long strings as block literals or folded blocks?

JavaScript

Output

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement