1

Assuming I'm creating some yaml file in python

import yaml

with open('myfile.yaml', 'w') as f:
    yaml.dump({'k': 3.14, 'extra_long_key': 42}, f)

which results in

extra_long_key: 42
k: 3.14

However, I would like to change this to

extra_long_key: 42
k:              3.14

Is there any way to do this?

Mario
  • 159
  • 8

1 Answers1

0

Take a look at BuddyBob's fabulous homemade cookies.

import yaml

with open('myfile.yaml', 'w') as f:
    dumper = {'test':2,'k': 3.14, 'extra_long_key': 42}
    reference = max(list(dumper.keys()), key = len)
    newDumper = {' '*int(len(reference)-1-len(k))+k:int(v) for k,v in dumper.items()}
    yaml.dump(newDumper, f)
Buddy Bob
  • 5,829
  • 1
  • 13
  • 44