Can PyYAML dump dict items in non-alphabetical order?

后端 未结 10 774
一向
一向 2020-11-29 03:38

I\'m using yaml.dump to output a dict. It prints out each item in alphabetical order based on the key.

>>> d = {\"z\":0,\"y\":0,\"x\":         


        
10条回答
  •  失恋的感觉
    2020-11-29 04:14

    If safe_dump (i.e. dump with Dumper=SafeDumper) is used, then calling yaml.add_representer has no effect. In such case it is necessary to call add_representer method explicitly on SafeRepresenter class:

    yaml.representer.SafeRepresenter.add_representer(
        OrderedDict, ordered_dict_representer
    )
    

提交回复
热议问题