How to prevent YAML to dump long line without new line

21.1k views Asked by At

While dumping/serializing data having long lines in input, pyyaml adds extra indentation with new line - which is annoying, how can we avoid this conversion in two lines / multiple lines ?

e.g.

In [1]: x = "-c /home/user/test/test2/test23/tet/2s/test1/stest/longdirectory1/directory2/ --optnion12 --verbose"
In [2]: import yaml
In [3]: print (yaml.dump([dict(ATTRIBUTES=[dict(CONFIG=x)])], default_flow_style=False))

WRONG ONE

- ATTRIBUTES:
  - CONFIG: -c /home/user/test/test2/test23/tet/2s/test1/stest/longdirectory1/directory2/
      --optnion12 --verbose

Which should be like

- ATTRIBUTES:
  - CONFIG: -c /home/user/test/test2/test23/tet/2s/test1/stest/longdirectory1/directory2/ --optnion12 --verbose
1

There are 1 answers

3
shahjapan On BEST ANSWER

Thanks @MathieuMarques for suggesting to look @ dump options and link provided, YAML documentation was not good enough to find it out.

Anyways solution is to specify width parameter for dump function.

i.e. yaml.dump(data, width=1000)

A better approach suggested by @RandomCoder to use yaml.dump(data, width=float("inf")) for a permanent solution.