Created
October 10, 2022 03:10
-
-
Save wonhyeongseo/059da4759ed91f10ce6cff8e53934e46 to your computer and use it in GitHub Desktop.
Sort yaml alphabetically
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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