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
が一番簡単な気がする。