網(wǎng)站首頁 編程語言 正文
一、安裝
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) #
name | #age | #height | #weight | #
---|---|---|---|
autofelix | #25 | #174 | #65 | #
大神 | #23 | #164 | #55 | #
飛兔小哥 | #27 | #184 | #69.5 | #
八、復(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 | # +...........+.....+........+........+
原文鏈接:https://blog.51cto.com/autofelix/5160613
相關(guān)推薦
- 2022-05-11 RabbitMq工作模式深度剖析與Spring整合MQ
- 2022-04-26 JQuery實(shí)現(xiàn)頁面彈出框_jquery
- 2022-06-28 python神經(jīng)網(wǎng)絡(luò)使用tensorflow構(gòu)建長短時(shí)記憶LSTM_python
- 2023-01-29 Python使用pandas導(dǎo)入xlsx格式的excel文件內(nèi)容操作代碼_python
- 2021-10-04 Flutter輸入框TextField屬性及監(jiān)聽事件介紹_Android
- 2022-03-17 C#實(shí)現(xiàn)多文件打包壓縮(.Net?Core)_C#教程
- 2022-09-18 IOS開發(fā)壓縮后圖片模糊問題解決_IOS
- 2022-09-14 C++中地圖按鍵排序?qū)崿F(xiàn)示例_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支