python使用prettytable內(nèi)置庫美化輸出表格
前言:
大多數(shù)時候,需要輸出的信息能夠比較整齊的輸出來,在使用mysql的時候,我們使用命令符之后,會輸出特別好看的表格,python的prettytable庫就是這么一個工具,可以幫助我們打印出好看的表格,并且對中文支持特別友好
安裝
prettytable是pyhton內(nèi)置庫,通過命令直接可以安裝
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)

按行添加數(shù)據(jù)table.add_row()
還可以實現(xiàn)按列添加數(shù)據(jù)table.add_column()
這里會有一些不一樣的地方:
先使用**PrettyTable()**創(chuàng)建好表格,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文件添加數(shù)據(jù),并打印出表格
目前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導(dǎo)入數(shù)據(jù)
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()

到此這篇關(guān)于python使用prettytable內(nèi)置庫美化輸出表格的文章就介紹到這了,更多相關(guān)python輸出表格內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- python中prettytable庫的使用方法
- Python prettytable模塊應(yīng)用詳解
- Python利用prettytable實現(xiàn)格式化輸出內(nèi)容
- python?利用?PrettyTable?美化表格
- Python利用prettytable庫輸出好看的表格
- Python第三方包PrettyTable安裝及用法解析
- Python 使用 prettytable 庫打印表格美化輸出功能
- Python實用庫 PrettyTable 學(xué)習(xí)筆記
- python PrettyTable模塊的安裝與簡單應(yīng)用
- Python的PrettyTable模塊的方法實現(xiàn)
相關(guān)文章
python3中關(guān)于excel追加寫入格式被覆蓋問題(實例代碼)
這篇文章主要介紹了python3中關(guān)于excel追加寫入格式被覆蓋問題,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2020-01-01
TensorFlow查看輸入節(jié)點和輸出節(jié)點名稱方式
今天小編就為大家分享一篇TensorFlow查看輸入節(jié)點和輸出節(jié)點名稱方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01
asyncio 的 coroutine對象 與 Future對象使用指南
asyncio是Python 3.4版本引入的標(biāo)準庫,直接內(nèi)置了對異步IO的支持。asyncio的編程模型就是一個消息循環(huán)。今天我們就來詳細討論下asyncio 中的 coroutine 與 Future對象2016-09-09

