16
In [136]: a = [1,2,3,4,5]

In [137]: print yaml.dump(a)
[1, 2, 3, 4, 5]


In [138]: a = [1,2,3,4,5, [1,2,3]]

In [139]: print yaml.dump(a)
- 1
- 2
- 3
- 4
- 5
- [1, 2, 3]

why are the outputs of above two dumps different? Is it possible to force pyYAML to split the list always?

1
  • What are you expecting the output to look like? The corresponding dump of this python list looks fine in yaml format. Commented Dec 24, 2012 at 19:09

1 Answer 1

25

From the documentation:

print yaml.dump(a, default_flow_style=False)

The value can be True, False, or None. If None or unspecified (that is, the default), it chooses automatically whether to use inline or block-style output. False never uses inline, True is always inline.

2
  • Yep. As the first FAQ entry says: "By default, PyYAML chooses the style of a collection depending on whether it has nested collections. If a collection has nested collections, it will be assigned the block style. Otherwise it will have the flow style."
    – DSM
    Commented Dec 24, 2012 at 19:11
  • 7
    I can't choose either. I want lists to go out as [1,2,3] and dicts to go out in separate lines with indents. How can this be done?
    – Gulzar
    Commented Jun 11, 2019 at 12:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.