網站首頁 編程語言 正文
一、安裝
pip install PrettyTable
二、按行設置數據
import prettytable as pt # 按行添加數據 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 # 按行添加數據 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.add_column('sex',['男', '女', '男']) print(tb) # +-----------+-----+--------+--------+-----+ # | name | age | height | weight | sex | # +-----------+-----+--------+--------+-----+ # | autofelix | 25 | 174 | 65 | 男 | # | 大神 | 23 | 164 | 55 | 女 | # | 飛兔小哥 | 27 | 184 | 69.5 | 男 | # +-----------+-----+--------+--------+-----+
四、輸出風格
- MSWORD_FRIENDLY:MSWORD_FRIENDLY輸出風格
- PLAIN_COLUMNS:PLAIN_COLUMNS輸出風格
- RANDOM:每次隨機輸出風格
- DEFAULT:默認輸出風格
import prettytable as pt # 按行添加數據 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.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 # 按行添加數據 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 | # +----------+-----+
六、表格樣式設置
import prettytable as pt # 按行添加數據 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.align = 'l' # 設定數字輸出格式 tb.float_format = '2.2' # 設定邊框連接符為'*" tb.junction_char = '*' # 設定排序方式 tb.sortby = 'age' # 設定左側不填充空白字符 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 # 按行添加數據 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 | #
八、復制
import prettytable as pt # 按行添加數據 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
相關推薦
- 2022-07-23 .Net創建型設計模式之工廠方法模式(Factory?Method)_基礎應用
- 2022-10-04 golang中time包之時間間隔格式化和秒、毫秒、納秒等時間戳格式輸出的方法實例_Golang
- 2023-04-08 C語言字符串左旋的兩種實現方法_C 語言
- 2022-09-15 python?Pandas庫read_excel()參數實例詳解_python
- 2022-07-23 .Net創建型設計模式之簡單工廠模式(Simple?Factory)_基礎應用
- 2023-01-12 keepalived?+?nginx?實現高可用方案_nginx
- 2022-07-28 C++實例講解引用的使用_C 語言
- 2022-12-09 python淺拷貝與深拷貝使用方法詳解_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支