Readably print out a python dict() sorted by key

前端 未结 8 2182
悲&欢浪女
悲&欢浪女 2020-12-04 17:19

I would like to print a python dictionary to a file using PrettyPrinter (for human readability) but have the dictionary be sorted by key in the output file to further improv

8条回答
  •  抹茶落季
    2020-12-04 17:49

    I had the same problem you had. I used a for loop with the sorted function passing in the dictionary like so:

    for item in sorted(mydict):
        print(item)
    

提交回复
热议问题