Python實(shí)用庫(kù) PrettyTable 學(xué)習(xí)筆記
本文實(shí)例講述了Python實(shí)用庫(kù) PrettyTable。分享給大家供大家參考,具體如下:
PrettyTable安裝
使用pip即可十分方便的安裝PrettyTable,如下:
pip install PrettyTable
PrettyTable使用示例
github上有PrettyTable的使用說(shuō)明,鏈接如下:https://github.com/dprince/python-prettytable
以下是具體的使用示例:
import prettytable as pt
按行添加數(shù)據(jù)
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 |
+-----------+------+------------+-----------------+
按列添加數(shù)據(jù)
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 |
+-----------+------+------------+-----------------+-------+
使用不同的輸出風(fēng)格
tb.set_style(pt.MSWORD_FRIENDLY) print('--- style:MSWORD_FRIENDLY -----') print(tb) tb.set_style(pt.PLAIN_COLUMNS) print('--- style:PLAIN_COLUMNS -----') print(tb)
隨機(jī)風(fēng)格,每次不同
tb.set_style(pt.RANDOM) print('--- style:MSWORD_FRIENDLY -----') print(tb) tb.set_style(pt.DEFAULT) print('--- style:DEFAULT -----') print(tb)
--- style:MSWORD_FRIENDLY -----
| 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 |
--- style:PLAIN_COLUMNS -----
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
--- style:MSWORD_FRIENDLY -----
@ Adelaide 1295 1158259 600.5 @
@ Brisbane 5905 1857594 1146.4@
@ Darwin 112 120900 1714.7@
@ Hobart 1357 205556 619.5 @
--- style:DEFAULT -----
+-----------+------+------------+-----------------+
| 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 |
+-----------+------+------------+-----------------+
不打印,獲取表格字符串
s = tb.get_string() print(s)
可以只獲取指定列或行
s = tb.get_string(fields=["City name", "Population"],start=1,end=4) print(s)
+-----------+------+------------+-----------------+
| 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 |
+-----------+------+------------+-----------------+
+-----------+------------+
| City name | Population |
+-----------+------------+
| Brisbane | 1857594 |
| Darwin | 120900 |
| Hobart | 205556 |
+-----------+------------+
自定義表格輸出樣式
設(shè)定左對(duì)齊
tb.align = 'l'
設(shè)定數(shù)字輸出格式
tb.float_format = "2.2"
設(shè)定邊框連接符為'*”
tb.junction_char = "*"
設(shè)定排序方式
tb.sortby = "City name"
設(shè)定左側(cè)不填充空白字符
tb.left_padding_width = 0 print(tb)
*----------*-----*-----------*----------------*
|City name |Area |Population |Annual Rainfall |
*----------*-----*-----------*----------------*
|Adelaide |1295 |1158259 |600.50 |
|Brisbane |5905 |1857594 |1146.40 |
|Darwin |112 |120900 |1714.70 |
|Hobart |1357 |205556 |619.50 |
*----------*-----*-----------*----------------*
不顯示邊框
tb.border = 0 print(tb)
修改邊框分隔符
tb.set_style(pt.DEFAULT) tb.horizontal_char = '+' print(tb)
City name Area Population Annual Rainfall
Adelaide 1295 1158259 600.50
Brisbane 5905 1857594 1146.40
Darwin 112 120900 1714.70
Hobart 1357 205556 619.50
+++++++++++++++++++++++++++++++++++++++++++++++++++
| City name | Area | Population | Annual Rainfall |
+++++++++++++++++++++++++++++++++++++++++++++++++++
| Adelaide | 1295 | 1158259 | 600.50 |
| Brisbane | 5905 | 1857594 | 1146.40 |
| Darwin | 112 | 120900 | 1714.70 |
| Hobart | 1357 | 205556 | 619.50 |
+++++++++++++++++++++++++++++++++++++++++++++++++++
prettytable也支持輸出HTML代碼
s = tb.get_html_string() print(s)
City name | Area | Population | Annual Rainfall |
---|---|---|---|
Adelaide | 1295 | 1158259 | 600.50 |
Brisbane | 5905 | 1857594 | 1146.40 |
Darwin | 112 | 120900 | 1714.70 |
Hobart | 1357 | 205556 | 619.50 |
使用copy方法復(fù)制對(duì)象
tb.set_style(pt.DEFAULT) tb.horizontal_char = '.' tb2 = tb.copy() tb.align = 'l' tb2.align = 'r' print(tb) print(tb2)
直接賦值,得到的是索引
tb.horizontal_char = '-' tb.aliign = 'l' tb3 = tb tb3.align = 'r' print(tb) print(tb3)
+...........+......+............+.................+
| City name | Area | Population | Annual Rainfall |
+...........+......+............+.................+
| Adelaide | 1295 | 1158259 | 600.50 |
| Brisbane | 5905 | 1857594 | 1146.40 |
| Darwin | 112 | 120900 | 1714.70 |
| Hobart | 1357 | 205556 | 619.50 |
+...........+......+............+.................+
+...........+......+............+.................+
| City name | Area | Population | Annual Rainfall |
+...........+......+............+.................+
| Adelaide | 1295 | 1158259 | 600.50 |
| Brisbane | 5905 | 1857594 | 1146.40 |
| Darwin | 112 | 120900 | 1714.70 |
| Hobart | 1357 | 205556 | 619.50 |
+...........+......+............+.................+
+-----------+------+------------+-----------------+
| City name | Area | Population | Annual Rainfall |
+-----------+------+------------+-----------------+
| Adelaide | 1295 | 1158259 | 600.50 |
| Brisbane | 5905 | 1857594 | 1146.40 |
| Darwin | 112 | 120900 | 1714.70 |
| Hobart | 1357 | 205556 | 619.50 |
+-----------+------+------------+-----------------+
+-----------+------+------------+-----------------+
| City name | Area | Population | Annual Rainfall |
+-----------+------+------------+-----------------+
| Adelaide | 1295 | 1158259 | 600.50 |
| Brisbane | 5905 | 1857594 | 1146.40 |
| Darwin | 112 | 120900 | 1714.70 |
| Hobart | 1357 | 205556 | 619.50 |
+-----------+------+------------+-----------------+
---------------------
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python操作Excel表格技巧總結(jié)》、《Python文件與目錄操作技巧匯總》、《Python文本文件操作技巧匯總》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門(mén)與進(jìn)階經(jīng)典教程》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
- Python prettytable模塊應(yīng)用詳解
- Python利用prettytable實(shí)現(xiàn)格式化輸出內(nèi)容
- python使用prettytable內(nèi)置庫(kù)美化輸出表格
- python?利用?PrettyTable?美化表格
- Python利用prettytable庫(kù)輸出好看的表格
- Python第三方包PrettyTable安裝及用法解析
- Python 使用 prettytable 庫(kù)打印表格美化輸出功能
- python PrettyTable模塊的安裝與簡(jiǎn)單應(yīng)用
- python中prettytable庫(kù)的使用方法
相關(guān)文章
python爬蟲(chóng) requests-html的使用
這篇文章主要介紹了python爬蟲(chóng) requests-html的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11requests庫(kù)發(fā)送http請(qǐng)求的示例代碼
這篇文章主要介紹了Python?requests發(fā)送http請(qǐng)求的相關(guān)知識(shí),requests是一個(gè)Python的第三方庫(kù),用于發(fā)送HTTP請(qǐng)求,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-12-12python爬蟲(chóng)入門(mén)教程--利用requests構(gòu)建知乎API(三)
這篇文章主要給大家介紹了關(guān)于python爬蟲(chóng)入門(mén)之利用requests構(gòu)建知乎API的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-05-05Python網(wǎng)絡(luò)爬蟲(chóng)信息提取mooc代碼實(shí)例
這篇文章主要介紹了python網(wǎng)絡(luò)爬蟲(chóng)與信息提取mooc,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03python判斷all函數(shù)輸出結(jié)果是否為true的方法
在本篇內(nèi)容里小編給各位整理的是一篇關(guān)于python判斷all函數(shù)輸出結(jié)果是否為true的方法,有需要的朋友們可以學(xué)習(xí)下。2020-12-12Django使用Profile擴(kuò)展User模塊方式
這篇文章主要介紹了Django使用Profile擴(kuò)展User模塊方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05