python?利用?PrettyTable?美化表格
更新時間:2022年04月02日 11:29:37 作者:autofelix
這篇文章主要介紹了python?利用?PrettyTable?美化表格,首先按行設(shè)置數(shù)據(jù)展開相關(guān)內(nèi)容,需要的小伙伴可以參考一下
一、安裝
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è)定左對齊 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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:
- python中prettytable庫的使用方法
- Python prettytable模塊應(yīng)用詳解
- Python利用prettytable實現(xiàn)格式化輸出內(nèi)容
- python使用prettytable內(nèi)置庫美化輸出表格
- Python利用prettytable庫輸出好看的表格
- Python第三方包PrettyTable安裝及用法解析
- Python 使用 prettytable 庫打印表格美化輸出功能
- Python實用庫 PrettyTable 學(xué)習(xí)筆記
- python PrettyTable模塊的安裝與簡單應(yīng)用
- Python的PrettyTable模塊的方法實現(xiàn)
相關(guān)文章
python中利用Future對象異步返回結(jié)果示例代碼
future是一種對象,表示異步執(zhí)行的操作。下面這篇文章主要給大家介紹了關(guān)于python中利用Future對象異步返回結(jié)果的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-09-09
關(guān)于python與opc ua Expert endpoint連接的問題
這篇文章主要介紹了關(guān)于python與opc ua Expert endpoint連接的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02
Python解析json時提示“string indices must be integers”問題解決方法
這篇文章主要介紹了Python解析json時提示“string indices must be integers”問題解決方法,結(jié)合實例形式分析了Python解析json字符串操作規(guī)范與相關(guān)使用技巧,需要的朋友可以參考下2019-07-07

