Skip to content

Instantly share code, notes, and snippets.

@wonhyeongseo
Created October 10, 2022 03:10
Show Gist options
  • Save wonhyeongseo/059da4759ed91f10ce6cff8e53934e46 to your computer and use it in GitHub Desktop.
Save wonhyeongseo/059da4759ed91f10ce6cff8e53934e46 to your computer and use it in GitHub Desktop.
Sort yaml alphabetically
# pip install pyyaml
import yaml
# raw file: https://github.com/OpenBB-finance/OpenBBTerminal/blob/main/website/data/menu/main.yml
with open('main.yml', 'rt', encoding='utf8') as f:
content = yaml.safe_load(f.read())
content = content['main'][0]
def sort_nested(l: list):
result = []
for d in sorted(l, key=lambda d: d['name'].lower()):
if d.get('sub'):
d['sub'] = sort_nested(d['sub'])
result.append(d)
return result
with open('new.yml', 'wt', encoding='utf8') as f:
yaml.safe_dump(sort_nested(content['sub']))
# some manual work was needed to copy the
# remainder of main.yml to new.yml and format the document.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment