亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python實(shí)用庫(kù) PrettyTable 學(xué)習(xí)筆記

 更新時(shí)間:2019年08月06日 11:59:19   作者:山那邊的瘦子  
這篇文章主要介紹了Python實(shí)用庫(kù) PrettyTable 學(xué)習(xí)筆記,結(jié)合實(shí)例形式分析了Python表格操作庫(kù)PrettyTable的安裝、使用技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下

本文實(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ì)有所幫助。

相關(guān)文章

  • python爬蟲(chóng) requests-html的使用

    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-11
  • jupyter notebook 增加kernel教程

    jupyter notebook 增加kernel教程

    這篇文章主要介紹了jupyter notebook 增加kernel教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-04-04
  • requests庫(kù)發(fā)送http請(qǐng)求的示例代碼

    requests庫(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-12
  • django的登錄注冊(cè)系統(tǒng)的示例代碼

    django的登錄注冊(cè)系統(tǒng)的示例代碼

    這篇文章主要介紹了django的登錄注冊(cè)系統(tǒng)的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-05-05
  • 跟老齊學(xué)Python之折騰一下目錄

    跟老齊學(xué)Python之折騰一下目錄

    本講只關(guān)注os.path,真所謂“弱水三千,只取一瓢”,為什么這么偏愛(ài)它呢?因?yàn)樗颓懊嬉呀?jīng)講過(guò)的文件操作進(jìn)行配合,就能夠隨心所欲操作各個(gè)地方的文件了
    2014-10-10
  • Python中如何給字典設(shè)置默認(rèn)值

    Python中如何給字典設(shè)置默認(rèn)值

    這篇文章主要介紹了Python中如何給字典設(shè)置默認(rèn)值問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • python爬蟲(chóng)入門(mén)教程--利用requests構(gòu)建知乎API(三)

    python爬蟲(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-05
  • Python網(wǎng)絡(luò)爬蟲(chóng)信息提取mooc代碼實(shí)例

    Python網(wǎng)絡(luò)爬蟲(chóng)信息提取mooc代碼實(shí)例

    這篇文章主要介紹了python網(wǎng)絡(luò)爬蟲(chóng)與信息提取mooc,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • python判斷all函數(shù)輸出結(jié)果是否為true的方法

    python判斷all函數(shù)輸出結(jié)果是否為true的方法

    在本篇內(nèi)容里小編給各位整理的是一篇關(guān)于python判斷all函數(shù)輸出結(jié)果是否為true的方法,有需要的朋友們可以學(xué)習(xí)下。
    2020-12-12
  • Django使用Profile擴(kuò)展User模塊方式

    Django使用Profile擴(kuò)展User模塊方式

    這篇文章主要介紹了Django使用Profile擴(kuò)展User模塊方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-05-05

最新評(píng)論