Can PyYAML dump dict items in non-alphabetical order?

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

    I was also looking for an answer to the question "how to dump mappings with the order preserved?" I couldn't follow the solution given above as i am new to pyyaml and python. After spending some time on the pyyaml documentation and other forums i found this.

    You can use the tag

    !!omap

    to dump the mappings by preserving the order. If you want to play with the order i think you have to go for keys:values

    The links below can help for better understanding.

    https://bitbucket.org/xi/pyyaml/issue/13/loading-and-then-dumping-an-omap-is-broken

    http://yaml.org/type/omap.html

提交回复
热议问题