Bonky Zhu
If someone is able to show me that what I think or do is not right, I will happily change, for I seek the truth, by which no one was ever truly harmed. It is the person who continues in his self-deception and ignorance who is harmed.

如何使用Python打印出漂亮的表格

回想一下,你是怎样利用Python把你想要打印的东西利用表格输出?我想很多人都是通过下面这种方式:

print(f"{num}\t{name}\t{proprerty}\n"


这样子生成的表格虽然有表格的模样,但的确不是很美观。所以,我们可以利用 PrettyTable 这个包,使你的表格更加美观。下面是我在写操作系统实验使用的一个示例:

from prettytable import PrettyTable

Process0 = {'ID':0,'PRIORITY':9, 'CPUTIME':0, 'ALLTIME':3, 'STARTBLOCK':2, 'BLOCKTIME':3, 'STATE':'READY'}
....省略....
All_Process = [ Process0,Process1,Process2,Process3,Process4 

table = PrettyTable(list(Process0.keys()))
for i in range(5):
    table.add_row(list(All_Process[i].values()))


打印出来的效果是这样子的:

屏幕快照 2019-10-31 下午10.13.02

是不是变漂亮了很多~科科 ?

Share

You may also like...

发表评论