python表格方式打印数据

在python脚本里如果想简单的print数据出来,但又想有table的效果,怎么办?   一开始就是自己尝试打印table,但最直接的问题就是—- ||| 对不齐。 

如果想对其表格方式打印,那么就需要补长减短,这样就能保证数据的整齐打印。  我这里就不造轮子了,直接用别人的开发好的模块。


原文地址, http://xiaorui.cc/?p=2919


这次使用的是prettytable模块,他主要有两大参数:

add_row,是一行行的添加数据。 
add_column, 是一列列的添加数据,也就是你穿进去的list,会横着显示出来。 

模块地址: https://code.google.com/archive/p/prettytable/

废话不多说,直接上例子:

#http://xiaorui.cc

#coding:utf-8
from prettytable import PrettyTable

a = {'20160202': 48519,
 '20160203': 65883,
 '20160206': 2290,
 '20160210': 2115,
 '20160212': 2406,
 '20160213': 2105}

for i,j in a.items():
    print '-----------------------------'
    print '|__%s__|_____%s_____|'%(i,j)


x = PrettyTable(["datetime","count" ])
x.padding_width = 1
for i,j in a.items():
    x.add_row([i,j])
print x


col = PrettyTable()
col.add_column("City name",["Adelaide","Brisbane","Darwin","Hobart","Sydney","Melbourne","Perth"])
col.add_column("Area", [1295, 5905, 112, 1357, 2058, 1566, 5386])
col.add_column("Population", [1158259, 1857594, 120900, 205556, 4336374, 3806092, 1554769])
col.add_column("Annual Rainfall",[600.5, 1146.4, 1714.7, 619.5, 1214.8, 646.9, 869.4])
print col

mix = PrettyTable()
mix.field_names = ["City name", "Area"]
mix.add_row(["Adelaide",1295])
mix.add_row(["Brisbane",5905])
mix.add_row(["Darwin", 112])
mix.add_row(["Hobart", 1357])
mix.add_row(["Sydney", 2058])
mix.add_row(["Melbourne", 1566])
mix.add_row(["Perth", 5386])
mix.add_column("Population", [1158259, 1857594, 120900, 205556, 4336374, 3806092, 1554769])
mix.add_column("Annual Rainfall",[600.5, 1146.4, 1714.7, 619.5, 1214.8, 646.9, 869.4])

print mix

上面python代码运行后的结果…    有些遗憾的是他没有颜色的效果,有时间我会对这代码进行改造,加入颜色的选择参数。 

#xiaorui.cc
Press ENTER or type command to continue
-----------------------------
|__20160212__|_____2406_____|
-----------------------------
|__20160213__|_____2105_____|
-----------------------------
|__20160210__|_____2115_____|
-----------------------------
|__20160206__|_____2290_____|
-----------------------------
|__20160203__|_____65883_____|
-----------------------------
|__20160202__|_____48519_____|

+----------+-------+
| datetime | count |
+----------+-------+
| 20160212 |  2406 |
| 20160213 |  2105 |
| 20160210 |  2115 |
| 20160206 |  2290 |
| 20160203 | 65883 |
| 20160202 | 48519 |
+----------+-------+
+-----------+------+------------+-----------------+
| City name | Area | Population | Annual Rainfall |
+-----------+------+------------+-----------------+
|  Adelaide | 1295 |  1158259   |      600.5      |
|  Brisbane | 5905 |  1857594   |      1146.4     |
|   Darwin  | 112  |   120900   |      1714.7     |
|   Hobart  | 1357 |   205556   |      619.5      |
|   Sydney  | 2058 |  4336374   |      1214.8     |
| Melbourne | 1566 |  3806092   |      646.9      |
|   Perth   | 5386 |  1554769   |      869.4      |
+-----------+------+------------+-----------------+
+-----------+------+------------+-----------------+
| City name | Area | Population | Annual Rainfall |
+-----------+------+------------+-----------------+
|  Adelaide | 1295 |  1158259   |      600.5      |
|  Brisbane | 5905 |  1857594   |      1146.4     |
|   Darwin  | 112  |   120900   |      1714.7     |
|   Hobart  | 1357 |   205556   |      619.5      |
|   Sydney  | 2058 |  4336374   |      1214.8     |
| Melbourne | 1566 |  3806092   |      646.9      |
|   Perth   | 5386 |  1554769   |      869.4      |
+-----------+------+------------+-----------------+

python打印表格table的方式有很多,我这边就不多扯淡了。  有兴趣的朋友可以在github搜搜。 另外在github中没找到既能输出table表格,又能显示特定color的库。 

END.


大家觉得文章对你有些作用! 如果想赏钱,可以用微信扫描下面的二维码,感谢!
另外再次标注博客原地址  xiaorui.cc

发表评论

邮箱地址不会被公开。 必填项已用*标注