12

I use the load_yaml_guess_indent(f, preserve_quotes=True) to read a YAML file, then modify it and write it back. I noticed long lines are getting wrapped when they are written back. (A line break is inserted after 80-85 characters.) Is there a parameter I can pass to stop this from happening?

keheliya
  • 2,393
  • 2
  • 24
  • 35

1 Answers1

15

These are the parameters you can hand in to round_trip_dump():

def round_trip_dump(data, stream=None, Dumper=RoundTripDumper,
                default_style=None, default_flow_style=None,
                canonical=None, indent=None, width=None,
                allow_unicode=None, line_break=None,
                encoding=enc, explicit_start=None, explicit_end=None,
                version=None, tags=None, block_seq_indent=None,
                top_level_colon_align=None, prefix_colon=None):

The one you are looking for is width

If you are using the new (ruamel.yaml >= 0.15 ) API, most of these parameters directly translate to attributes on the YAML instance you create, so in that case you would do:

yaml = ruamel.yaml.YAML()
yaml.width = 4096  # or some other big enough value to prevent line-wrap
yaml.dump(data, stream=your_stream)
Anthon
  • 69,918
  • 32
  • 186
  • 246
  • 1
    @KatrinaBrock Done – Anthon Oct 13 '17 at 22:00
  • 1
    @JeffHykin That is because the line width (an integer) is compared to the value provided, and that value is not "cast" to an an integer. I.e. in Python `80 < float("Infinity")` gives `True` but `int(float("Infinity"))` throws an overflow error. – Anthon Aug 02 '19 at 21:46
  • Is it possible to preserve existing line wrapping regardless of width? Fixed line width causes either wrapping or unwrapping if existing line wrappings are inconsistent, and I'd like to reduce whitespace noise – Pat Myron Mar 28 '22 at 06:25