0%

Python将字典打印为带有标题的表

1
2
3
Key________Label__________Number
1__________Spices_________42
2__________Other Stuff______16

✔️ 可以使用string formatting:

1
2
3
4
print "{:<8} {:<15} {:<10}".format('Key','Label','Number')
for k, v in d.iteritems():
label, num = v
print "{:<8} {:<15} {:<10}".format(k, label, num)

Output:

1
2
3
4
5
6
7
Key      Label           Number    
1 Spices 38.0
2 Other stuff 24.0
3 Tea 44.0
5 Fruit 5.0
6 Textiles 37.0
10 Contraband 1000.0