I'm writing a YAML file using the yaml
library in Python 3, and I'd like to choose where it puts the line breaks when writing a long block of text.
Here's a silly example of the kind of thing I'm trying to do. The days
entry is a long block of text with several items separated by commas. I'd like to keep each item together on a line, but in this example, "9 Ladies Dancing" gets split.
from yaml import safe_load, safe_dump
s = """\
- title: 12 Days of Christmas
- days: A partridge in a pear tree,
2 Turtle Doves,
3 French Hens,
4 Calling Birds,
5 Gold Rings,
6 Geese a-Laying,
7 Swans a-Swimming,
8 Maids a-Milking,
9 Ladies Dancing,
10 Lords a-Leaping,
11 Pipers Piping,
12 Drummers Drumming
"""
l = safe_load(s)
print(safe_dump(l, default_flow_style=False))
This prints out:
- title: 12 Days of Christmas
- days: A partridge in a pear tree, 2 Turtle Doves, 3 French Hens, 4 Calling Birds,
5 Gold Rings, 6 Geese a-Laying, 7 Swans a-Swimming, 8 Maids a-Milking, 9 Ladies
Dancing, 10 Lords a-Leaping, 11 Pipers Piping, 12 Drummers Drumming
I'd like to load the full text of days
in as a single line, but I want to print it out as several lines up to 80 characters wide to make the items easier to review for correctness. I want several items on a line, but I'd like to split the lines at a comma so items don't get split across lines.