安装yamlordereddictloader
pip install yamlordereddictloader
from collections import OrderedDict
a = OrderedDict({'one':1,'tow':{'w':'w', 's':'s'}})
path = XXX
with open(path, 'w+', encoding='utf-8') as f:
yaml.dump(a, f, Dumper=yamlordereddictloader.Dumper, default_flow_style=False, allow_unicode=True)
结果如下: