網站首頁 編程語言 正文
前言:
大多數時候,需要輸出的信息能夠比較整齊的輸出來,在使用mysql的時候,我們使用命令符之后,會輸出特別好看的表格,python的prettytable庫就是這么一個工具,可以幫助我們打印出好看的表格,并且對中文支持特別友好
安裝
prettytable是pyhton內置庫,通過命令直接可以安裝
pip install prettytable
案例
from prettytable import PrettyTable
table = PrettyTable(['姓名', 'ID', 'Salary'])
table.add_row(['Phil', '0001', '10000'])
table.add_row(['Joge', '0002', '30000'])
print(table)
按行添加數據table.add_row()
還可以實現按列添加數據table.add_column()
這里會有一些不一樣的地方:
先使用**PrettyTable()**創建好表格,add_column(x,[]),x為列名,[]后面的列表為每列的值
import sys
from prettytable import PrettyTable
table = PrettyTable()
table.add_column('姓名', ['Phil', 'Joge'])
table.add_column('ID',['0002', '0001'])
print(table)
從csv文件添加數據,并打印出表格
目前prettytable支持csv,不支持xlsx
from prettytable import PrettyTable
from prettytable import from_csv
table = PrettyTable()
file = open('test.csv','r')
table = from_csv(file)
print(table)
file.close()
從HTML導入數據
from prettytable import PrettyTable
from prettytable import from_html
html = '''
<table>
<str>
<tr>
<th>姓名</th>
<th>ID</th>
</tr>
<tr>
<td>Vergil</td>
<td>001</td>
</tr>
<tr>
<td>Dante</td>
<td>002</td>
</tr>
</table>'''
table = from_html(html)
print(table)
還有支持sql輸入,這里就不演示了
prettytable還支持自定義表格的樣式、表格切片、輸出指定的行等功能
這里演示下自定義表格:
from prettytable import PrettyTable
from prettytable import from_csv
table = PrettyTable()
file = open('test.csv','r')
table = from_csv(file)
table.border = True
table.junction_char = '%'
table.horizontal_char = '+'
table.vertical_char = '^'
print(table)
file.close()
原文鏈接:https://blog.csdn.net/KIKI_ZSH/article/details/124048459
相關推薦
- 2022-07-22 用C語言根據天數輸出對應的年、月、日
- 2023-12-22 獲取微信小程序版本號,uni
- 2022-11-10 Android使用popupWindow仿微信彈出框使用方法_Android
- 2022-07-18 SQL?Server中的數據類型詳解_MsSql
- 2022-06-12 python操作RabbitMq的三種工作模式_python
- 2022-09-07 GoLang使goroutine停止的五種方法實例_Golang
- 2022-06-17 C語言深入探索動態內存分配的使用_C 語言
- 2022-01-17 cookie localStorage sessionStorage 數據傳參方式
- 最近更新
-
- 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同步修改后的遠程分支