Autonumbering in YAML file

91 Views Asked by At

I have following YAML file with disrupted numbering:

aaa:
  bbb:
    ccc:
      1:
        ddd: '123'
        eee: '123'
        fff: 'abc'
        ggg: 'abc'
        hhh: '123'
     4:
        ddd: '123'
        eee: '123'
        fff: 'abc'
        ggg: 'abc'
        hhh: '123'
      5:
        ddd: '123'
        eee: '123'
        fff: 'abc'
        ggg: 'abc'
        hhh: '123'
      9:
        ddd: '123'
        eee: '123'
        fff: 'abc'
        ggg: 'abc'
        hhh: '123'

Is there a way to implement automatic numbering in Python 2.7 for such file? So that keys are in correct order?

The desired file then would be:

aaa:
  bbb:
    ccc:
      1:
        ddd: '123'
        eee: '123'
        fff: 'abc'
        ggg: 'abc'
        hhh: '123'
     2:
        ddd: '123'
        eee: '123'
        fff: 'abc'
        ggg: 'abc'
        hhh: '123'
      3:
        ddd: '123'
        eee: '123'
        fff: 'abc'
        ggg: 'abc'
        hhh: '123'
      4:
        ddd: '123'
        eee: '123'
        fff: 'abc'
        ggg: 'abc'
        hhh: '123'

I did not find such option in Python yaml library. Another option would be convert to JSON, but in this case it is not preferred - as I also need to save the comments.

0

There are 0 best solutions below