python?利用?PrettyTable?美化表格
一、安裝
pip install PrettyTable
二、按行設(shè)置數(shù)據(jù)
import prettytable as pt # 按行添加數(shù)據(jù) tb = pt.PrettyTable() tb.field_names = ['name', 'age', 'height', 'weight'] tb.add_row(['autofelix', 25, 174, 65]) tb.add_row(['大神', 23, 164, 55]) tb.add_row(['飛兔小哥', 27, 184, 69.5]) print(tb) # +-----------+-----+--------+--------+ # | name | age | height | weight | # +-----------+-----+--------+--------+ # | autofelix | 25 | 174 | 65 | # | 大神 | 23 | 164 | 55 | # | 飛兔小哥 | 27 | 184 | 69.5 | # +-----------+-----+--------+--------+
三、按列添加
import prettytable as pt # 按行添加數(shù)據(jù) tb = pt.PrettyTable() tb.field_names = ['name', 'age', 'height', 'weight'] tb.add_row(['autofelix', 25, 174, 65]) tb.add_row(['大神', 23, 164, 55]) tb.add_row(['飛兔小哥', 27, 184, 69.5]) # 按列添加數(shù)據(jù) tb.add_column('sex',['男', '女', '男']) print(tb) # +-----------+-----+--------+--------+-----+ # | name | age | height | weight | sex | # +-----------+-----+--------+--------+-----+ # | autofelix | 25 | 174 | 65 | 男 | # | 大神 | 23 | 164 | 55 | 女 | # | 飛兔小哥 | 27 | 184 | 69.5 | 男 | # +-----------+-----+--------+--------+-----+
四、輸出風(fēng)格
- MSWORD_FRIENDLY:MSWORD_FRIENDLY輸出風(fēng)格
- PLAIN_COLUMNS:PLAIN_COLUMNS輸出風(fēng)格
- RANDOM:每次隨機(jī)輸出風(fēng)格
- DEFAULT:默認(rèn)輸出風(fēng)格
import prettytable as pt # 按行添加數(shù)據(jù) tb = pt.PrettyTable() tb.field_names = ['name', 'age', 'height', 'weight'] tb.add_row(['autofelix', 25, 174, 65]) tb.add_row(['大神', 23, 164, 55]) tb.add_row(['飛兔小哥', 27, 184, 69.5]) # 風(fēng)格 tb.set_style(pt.MSWORD_FRIENDLY) print(tb) # | name | age | height | weight | # | autofelix | 25 | 174 | 65 | # | 大神 | 23 | 164 | 55 | # | 飛兔小哥 | 27 | 184 | 69.5 |
五、獲取字符串
import prettytable as pt # 按行添加數(shù)據(jù) tb = pt.PrettyTable() tb.field_names = ['name', 'age', 'height', 'weight'] tb.add_row(['autofelix', 25, 174, 65]) tb.add_row(['大神', 23, 164, 55]) tb.add_row(['飛兔小哥', 27, 184, 69.5]) # 不打印,獲取表格字符串 s1 = tb.get_string() print(s1) # +-----------+-----+--------+--------+ # | name | age | height | weight | # +-----------+-----+--------+--------+ # | autofelix | 25 | 174 | 65 | # | 大神 | 23 | 164 | 55 | # | 飛兔小哥 | 27 | 184 | 69.5 | # +-----------+-----+--------+--------+ # 或者可以只獲取指定列或行 s2 = tb.get_string(fields=['name', 'age'], start=1, end=4) print(s2) # +----------+-----+ # | name | age | # +----------+-----+ # | 大神 | 23 | # | 飛兔小哥 | 27 | # +----------+-----+
六、表格樣式設(shè)置
import prettytable as pt # 按行添加數(shù)據(jù) tb = pt.PrettyTable() tb.field_names = ['name', 'age', 'height', 'weight'] tb.add_row(['autofelix', 25, 174, 65]) tb.add_row(['大神', 23, 164, 55]) tb.add_row(['飛兔小哥', 27, 184, 69.5]) # 設(shè)定左對(duì)齊 tb.align = 'l' # 設(shè)定數(shù)字輸出格式 tb.float_format = '2.2' # 設(shè)定邊框連接符為'*" tb.junction_char = '*' # 設(shè)定排序方式 tb.sortby = 'age' # 設(shè)定左側(cè)不填充空白字符 tb.left_padding_width = 0 # 不顯示邊框 # tb.border = 0 # 修改邊框分隔符 tb.horizontal_char = '+' print(tb) # *++++++++++*++++*+++++++*+++++++* # |name |age |height |weight | # *++++++++++*++++*+++++++*+++++++* # |大神 |23 |164 |55 | # |autofelix |25 |174 |65 | # |飛兔小哥 |27 |184 |69.50 | # *++++++++++*++++*+++++++*+++++++*
七、輸出成HTML
import prettytable as pt # 按行添加數(shù)據(jù) tb = pt.PrettyTable() tb.field_names = ['name', 'age', 'height', 'weight'] tb.add_row(['autofelix', 25, 174, 65]) tb.add_row(['大神', 23, 164, 55]) tb.add_row(['飛兔小哥', 27, 184, 69.5]) # 輸出HTML代碼 s = tb.get_html_string() print(s) # <table> # <thead> # <tr> # <th>name</th> # <th>age</th> # <th>height</th> # <th>weight</th> # </tr> # </thead> # <tbody> # <tr> # <td>autofelix</td> # <td>25</td> # <td>174</td> # <td>65</td> # </tr> # <tr> # <td>大神</td> # <td>23</td> # <td>164</td> # <td>55</td> # </tr> # <tr> # <td>飛兔小哥</td> # <td>27</td> # <td>184</td> # <td>69.5</td> # </tr> # </tbody> # </table>
八、復(fù)制
import prettytable as pt # 按行添加數(shù)據(jù) tb = pt.PrettyTable() tb.field_names = ['name', 'age', 'height', 'weight'] tb.add_row(['autofelix', 25, 174, 65]) tb.add_row(['大神', 23, 164, 55]) tb.add_row(['飛兔小哥', 27, 184, 69.5]) tb.horizontal_char = '.' tb2 = tb.copy() tb.align = 'l' tb2.align = 'r' print(tb) print(tb2) # +...........+.....+........+........+ # | name | age | height | weight | # +...........+.....+........+........+ # | autofelix | 25 | 174 | 65 | # | 大神 | 23 | 164 | 55 | # | 飛兔小哥 | 27 | 184 | 69.5 | # +...........+.....+........+........+ # +...........+.....+........+........+ # | name | age | height | weight | # +...........+.....+........+........+ # | autofelix | 25 | 174 | 65 | # | 大神 | 23 | 164 | 55 | # | 飛兔小哥 | 27 | 184 | 69.5 | # +...........+.....+........+........+
到此這篇關(guān)于python 利用 PrettyTable 美化表格的文章就介紹到這了,更多相關(guān)PrettyTable 美化表格內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python prettytable模塊應(yīng)用詳解
- Python利用prettytable實(shí)現(xiàn)格式化輸出內(nèi)容
- python使用prettytable內(nèi)置庫(kù)美化輸出表格
- Python利用prettytable庫(kù)輸出好看的表格
- Python第三方包PrettyTable安裝及用法解析
- Python 使用 prettytable 庫(kù)打印表格美化輸出功能
- Python實(shí)用庫(kù) PrettyTable 學(xué)習(xí)筆記
- python PrettyTable模塊的安裝與簡(jiǎn)單應(yīng)用
- python中prettytable庫(kù)的使用方法
相關(guān)文章
Python 解決相對(duì)路徑問題:"No such file or directory"
這篇文章主要介紹了Python 解決相對(duì)路徑問題:"No such file or directory"具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-06-06python的numpy模塊安裝不成功簡(jiǎn)單解決方法總結(jié)
這篇文章主要介紹了python的numpy模塊安裝不成功簡(jiǎn)單解決方法總結(jié),分享了四種python模塊導(dǎo)入不成功的解決方法,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12簡(jiǎn)單實(shí)現(xiàn)python畫圓功能
這篇文章主要為大家詳細(xì)介紹了簡(jiǎn)單實(shí)現(xiàn)python畫圓功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01python中取絕對(duì)值簡(jiǎn)單方法總結(jié)
在本篇內(nèi)容里小編給大家整理的是關(guān)于python中取絕對(duì)值簡(jiǎn)單方法,需要的朋友們可以學(xué)習(xí)下。2020-07-07科學(xué)計(jì)算NumPy之Ndarray運(yùn)算函數(shù)操作示例匯總
這篇文章主要為大家介紹了科學(xué)計(jì)算NumPy之Ndarray運(yùn)算函數(shù)操作示例匯總,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04關(guān)于numpy.where()函數(shù) 返回值的解釋
今天小編就為大家分享一篇關(guān)于numpy.where()函數(shù) 返回值的解釋,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2019-12-12django在接受post請(qǐng)求時(shí)顯示403forbidden實(shí)例解析
這篇文章主要介紹了django在接受post請(qǐng)求時(shí)顯示403forbidden實(shí)例解析,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01