python prettytable 打印表格

日常工作中會使用腳本統計數據,但數據輸出的格式展示經常讓人頭疼,要自己寫函數來實現。

於是爲了省時間和避免重複“造車輪”, 找到了 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      %
$+++++++++++$++++++$++++++++++++$+++++++++++++++++$

參考

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章