Any way to properly pretty-print ordered dictionaries?

后端 未结 15 1504
春和景丽
春和景丽 2020-12-07 14:34

I like the pprint module in Python. I use it a lot for testing and debugging. I frequently use the width option to make sure the output fits nicely within my terminal window

15条回答
  •  情歌与酒
    2020-12-07 14:49

    As a temporary workaround you can try dumping in JSON format. You lose some type information, but it looks nice and keeps the order.

    import json
    
    pprint(data, indent=4)
    # ^ugly
    
    print(json.dumps(data, indent=4))
    # ^nice
    

提交回复
热议问题