Can PyYAML dump dict items in non-alphabetical order?

后端 未结 10 868
一向
一向 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:37

    If you upgrade PyYAML to 5.1 version, now, it supports dump without sorting the keys like this:

    yaml.dump(data, sort_keys=False)
    

    As shown in help(yaml.Dumper), sort_keys defaults to True:

    Dumper(stream, default_style=None, default_flow_style=False,
      canonical=None, indent=None, width=None, allow_unicode=None,
      line_break=None, encoding=None, explicit_start=None, explicit_end=None,
      version=None, tags=None, sort_keys=True)
    

    (These are passed as kwargs to yaml.dump)

提交回复
热议问题