您好,登錄后才能下訂單哦!
這篇文章主要介紹“Python實(shí)用庫(kù)PrettyTable的用法”,在日常操作中,相信很多人在Python實(shí)用庫(kù)PrettyTable的用法問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”Python實(shí)用庫(kù)PrettyTable的用法”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
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實(shí)用庫(kù)PrettyTable的用法”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。