pythonyamlindentationpyyaml

Indent items of YAML lists when dumping


The context

Consider this minimal working example: I have this JSON file

$ cat main.json
[
  {
    "name": "a",
    "numbers": [1, 2]
  },
  {
    "name": "b",
    "numbers": [10, 20]
  }
]

I need to convert that JSON file to YAML. The following script accomplishes that

$ cat main.py
import json, yaml

input_file = open('main.json', 'r')

data_json = json.load(input_file)
data_yaml = yaml.dump(data_json)

print(data_yaml)

If we execute this script, we get

$ python main.py
- name: a
  numbers:
  - 1
  - 2
- name: b
  numbers:
  - 10
  - 20

The problem

I need items from a YAML list to be indented one level more. That is, the output should be

- name: a
  numbers:
    - 1
    - 2
- name: b
  numbers:
    - 10
    - 20

instead of

- name: a
  numbers:
  - 1
  - 2
- name: b
  numbers:
  - 10
  - 20

Additional context

Please note this is a minimal working example. Of course, I can solve this by manually inserting one more indent level or executing sed or any other tool to insert spaces in all those lines, but the problem is that I'm working with big files so it gets more complicated to check whether the substitution cover all cases, so I would rather use other methods.


Solution

  • You may consider using rumamel.yaml instead of PyYAML:

    pip install ruamel.yaml

    import json
    import sys
    
    import ruamel.yaml as yaml
    
    
    input_file = open('main.json', 'r')
    
    data_json = json.load(input_file)
    yaml = yaml.YAML()
    yaml.indent(sequence=4, offset=2)
    yaml.dump(data_json, sys.stdout)
    

    Output:

      - name: a
        numbers:
          - 1
          - 2
      - name: b
        numbers:
          - 10
          - 20