物理の駅 Physics station by 現役研究者

テクノロジーは共有されてこそ栄える

Python YAML形式でKeyでソートさせない

qiita.com

import yaml
from collections import OrderedDict
def represent_odict(dumper, instance):
    return dumper.represent_mapping('tag:yaml.org,2002:map', instance.items())
yaml.add_representer(OrderedDict, represent_odict)
obj = OrderedDict()
yaml.dump(obj, ...)

とかあるけど

import oyaml as yaml

が一番簡単な気がする。