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:
my_obj.short = "Hello"
my_obj.long = "Line1\nLine2\nLine3"
I'd like the YAML to look like this:
short: "Hello"
long: |
Line1
Line2
Line3
How can I instruct PyYAML to do this? If I call yaml.dump(my_obj)
, it produces a dict-like output:
{long: 'line1
line2
line3
', short: Hello}
(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.
Based on Any yaml libraries in Python that support dumping of long strings as block literals or folded blocks?
Output
You can use
ruamel.yaml
and its RoundTripLoader/Dumper (disclaimer: I am the author of that package) apart from doing what you want, it supports the YAML 1.2 specification (from 2009), and has several other improvements:gives:
(including the comment, starting in the same column as before)
You can also create this output starting from scratch, but then you do need to provide the extra information e.g. the explicit positions on where to fold.
Falling in love with @lbt's approach, I got this code:
It makes every multiline string be a block literal.
I was trying to avoid the monkey patching part. Full credit to @lbt and @J.F.Sebastian.
I wanted any input with a
\n
in it to be a block literal. Using the code inyaml/representer.py
as a base I got:Output