Print a dictionary into a table

前端 未结 6 1802
伪装坚强ぢ
伪装坚强ぢ 2020-12-18 06:46

I have a dictionary:

dic={\'Tim\':3, \'Kate\':2}

I would like to output it as:

Name Age
Tim 3
Kate 2

Is i

6条回答
  •  猫巷女王i
    2020-12-18 07:26

    You can do it this way,

    format = "{:<10}{:<10}"    
        print format.format("Name","Age")
        for name,age in dic.iteritems():
           print format.format(name,age)
    

    I have written a simple library to pretty print dictionary as a table https://github.com/varadchoudhari/Neat-Dictionary which uses a similar implementation

提交回复
热议问题