日常工作中会使用脚本统计数据,但数据输出的格式展示经常让人头疼,要自己写函数来实现。
于是为了省时间和避免重复“造车轮”, 找到了 prettytable 这个模块,github 地址。
一、安装
安装很简单,执行以下命令:
pip install prettytable
readme 文档:https://github.com/jazzband/prettytable/blob/master/README
二、示例
import prettytable as pt
# tb = pt.PrettyTable( ["City name", "Area", "Population", "Annual Rainfall"])
tb = pt.PrettyTable()
tb.field_names = ["City name", "Area", "Population", "Annual Rainfall"]
tb.add_row(["Adelaide",1295, 1158259, 600.5])
tb.add_row(["Brisbane",5905, 1857594, 1146.4])
tb.add_row(["Darwin", 112, 120900, 1714.7])
tb.add_row(["Hobart", 1357, 205556,619.5])
print(tb)
输出:
+-----------+------+------------+-----------------+
| 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 |
+-----------+------+------------+-----------------+
如果没有添加表头,那么会以默认的Field+编号显示,例如:
+---------+----------+----------+------------+
| Field 1 | Field 2 | Field 3 | Field 4 |
+---------+----------+----------+------------+
三、添加数据
1、添加行 table.add_row()
在上面简单的示例中,我们就是按行添加数据的。
添加的数据必须要是列表(list)的形式,而且数据的列表长度要和表头的长度一样。
tb.add_row(["Hobart", 1357, 205556,619.5])
2、添加列 table.add_column()
添加一列也很简单,直接在原代码中添加:
import prettytable as pt
# tb = pt.PrettyTable( ["City name", "Area", "Population", "Annual Rainfall"])
tb = pt.PrettyTable()
tb.field_names = ["City name", "Area", "Population", "Annual Rainfall"]
tb.add_row(["Adelaide",1295, 1158259, 600.5])
tb.add_row(["Brisbane",5905, 1857594, 1146.4])
tb.add_row(["Darwin", 112, 120900, 1714.7])
tb.add_row(["Hobart", 1357, 205556,619.5])
tb.add_column('index',[1,2,3,4]) #* 添加一列 *
print(tb)
输出:
+-----------+------+------------+-----------------+-------+
| City name | Area | Population | Annual Rainfall | index |
+-----------+------+------------+-----------------+-------+
| Adelaide | 1295 | 1158259 | 600.5 | 1 |
| Brisbane | 5905 | 1857594 | 1146.4 | 2 |
| Darwin | 112 | 120900 | 1714.7 | 3 |
| Hobart | 1357 | 205556 | 619.5 | 4 |
+-----------+------+------------+-----------------+-------+
3、从csv文件添加数据
PrettyTable 不仅提供了手动按行按列添加数据,也支持直接从 csv 文件中读取数据。
import prettytable as pt
from prettytable import from_csv
table = pt.PrettyTable()
with open('res.csv', 'r') as f:
table = from_csv(fp)
print(table)
注意:csv文件不能通过xls直接重命名得到,会报错。如果是xls文件,请用另存为csv获得csv文件
4、从sql查询值添加
从数据库查询出来的数据可以直接导入到表格打印,下面的例子使用了sqlite3,如果使用的是mysql也是一样的,只要能查询到数据就能导入到表格中
import sqlite3
from prettytable import from_db_cursor
conn = sqlite3.connect("/tmp/sqlite.db")
cur = conn.cursor()
cur.execute("SELECT * FROM res")
table = from_db_cursor(cur)
print(table)
5、从HTML导入数据
支持从html的表格中导入,请看下面这个例子:
from prettytable import from_html
html_string='''<table>
<tr>
<th>code</th>
<th>uuid</th>
<th>name</th>
<th>IP</th>
</tr>
<tr>
<td>1</td>
<td>server01</td>
<td>server-01</td>
<td>192.168.100.1</td>
</tr>
<tr>
<td>2</td>
<td>server02</td>
<td>server-02</td>
<td>192.168.100.2</td>
</tr>
</table>'''
table = from_html(html_string)
print(table[0])
导入 html 的表格,但是不一样的地方是 print 语句,使用 html 表格导入数据的时候 print 的必须是列表中的第一个元素,否则有可能会报[<prettytable.PrettyTable object at 0x7fa87feba590>]
这样的错误。
四、选择性输出
prettytable 在创建表格之后,你依然可以有选择的输出某些特定的行。
## 输出指定的列
print(table.get_string(fields=["Area", "Population"]))
## 输出指定的行,start 和 end 参数可以自由控制显示区间
print(table.get_string(start = 0, end = 2))
## 将表格切片
new_table = table[0:2]
print(new_table)
## 输出排序
print(table.get_string(sortby="City name", reversesort=True))
## 其中 reversesort 指定了是否倒序排序,默认为 False,即默认正序列排序。sortby 指定了排序的字段。
五、表格的样式
通过 set_style()
可以设置表格样式
import prettytable as pt
tb = pt.PrettyTable()
tb.field_names = ["City name", "Area", "Population", "Annual Rainfall"]
tb.add_row(["Adelaide",1295, 1158259, 600.5])
tb.add_row(["Brisbane",5905, 1857594, 1146.4])
tb.add_row(["Darwin", 112, 120900, 1714.7])
tb.add_row(["Hobart", 1357, 205556,619.5])
tb.set_style(pt.MSWORD_FRIENDLY)
print(tb)
输出:
| City name | Area | Population | Annual Rainfall | index |
| Adelaide | 1295 | 1158259 | 600.5 | 1 |
| Brisbane | 5905 | 1857594 | 1146.4 | 2 |
| Darwin | 112 | 120900 | 1714.7 | 3 |
| Hobart | 1357 | 205556 | 619.5 | 4 |
1、设置对齐方式
align
提供了用户设置对齐的方式,值有 l,r,c
方便代表 左对齐,右对齐和居中
如果不设置,默认居中对齐。
import prettytable as pt
tb = pt.PrettyTable()
tb.field_names = ["City name", "Area", "Population", "Annual Rainfall"]
tb.add_row(["Adelaide",1295, 1158259, 600.5])
tb.add_row(["Brisbane",5905, 1857594, 1146.4])
tb.add_row(["Darwin", 112, 120900, 1714.7])
tb.add_row(["Hobart", 1357, 205556,619.5])
tb.align["City name"] = "l"
tb.align["Area"] = "c"
tb.align["Population"] = "r"
tb.align["Annual Rainfall"] = "c"
print(tb)
输出:
+-----------+------+------------+-----------------+
| 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 |
+-----------+------+------------+-----------------+
2、控制边框样式
在 PrettyTable 中,边框由三个部分组成,横边框,竖边框,和边框连接符(横竖交叉的链接符号)
import prettytable as pt
tb = pt.PrettyTable()
tb.field_names = ["City name", "Area", "Population", "Annual Rainfall"]
tb.add_row(["Adelaide",1295, 1158259, 600.5])
tb.add_row(["Brisbane",5905, 1857594, 1146.4])
tb.add_row(["Darwin", 112, 120900, 1714.7])
tb.add_row(["Hobart", 1357, 205556,619.5])
tb.border = True
tb.junction_char='$'
tb.horizontal_char = '+'
tb.vertical_char = '%'
print(tb)
说明:
-
table.border
:控制是否显示边框,默认是 True -
table.junction_char
:控制边框连接符 -
table.horizontal_char
:控制横边框符号 -
table.vertical_char
:控制竖边框符号
输出:
$+++++++++++$++++++$++++++++++++$+++++++++++++++++$
% 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 %
$+++++++++++$++++++$++++++++++++$+++++++++++++++++$
网友评论