Any way to properly pretty-print ordered dictionaries?

后端 未结 15 1490
春和景丽
春和景丽 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:51

    def pprint_od(od):
        print "{"
        for key in od:
            print "%s:%s,\n" % (key, od[key]) # Fixed syntax
        print "}"
    

    There you go ^^

    for item in li:
        pprint_od(item)
    

    or

    (pprint_od(item) for item in li)
    

提交回复
热议问题