python3美化表格數(shù)據(jù)輸出結果的實現(xiàn)代碼
技術背景
在前面一篇博客中我們介紹過關于python的表格數(shù)據(jù)處理方案,這其中的工作重點就是對表格類型的數(shù)據(jù)進行梳理、計算和展示,本文重點介紹展示
這個方面的工作。首先我們看一個案例,定義一個數(shù)組形式的表格數(shù)據(jù):
[dechin@dechin-manjaro table]$ ipython Python 3.8.5 (default, Sep 4 2020, 07:30:14) Type 'copyright', 'credits' or 'license' for more information IPython 7.19.0 -- An enhanced Interactive Python. Type '?' for help. In [1]: table=[('a',1,2,3),('b',2,3,4)] In [2]: print(table) [('a', 1, 2, 3), ('b', 2, 3, 4)]
當我們直接打印這個表格數(shù)據(jù)的時候,發(fā)現(xiàn)效果非常的難看。雖然我們可以從這個表格中獲取到同樣的信息,但是這種數(shù)據(jù)展示的方法對于我們直接從打印輸出中獲取數(shù)據(jù)是非常不利的。
使用tabulate美化表格輸出
首先介紹一個工具tabulate,可以直接打印數(shù)組格式的表格數(shù)據(jù),并且有多種輸出格式可選。安裝方法同樣可以用pip來進行管理:
[dechin@dechin-manjaro table]$ python3 -m pip install tabulate Requirement already satisfied: tabulate in /home/dechin/anaconda3/lib/python3.8/site-packages (0.8.9)
安裝很容易,也沒有其他依賴。接下來我們用ipython來展示一些基本用法:
[dechin@dechin-manjaro table]$ ipython Python 3.8.5 (default, Sep 4 2020, 07:30:14) Type 'copyright', 'credits' or 'license' for more information IPython 7.19.0 -- An enhanced Interactive Python. Type '?' for help. In [1]: from tabulate import tabulate In [2]: import numpy as np In [3]: header=['index']+list(range(4)) # 表頭的定義 In [4]: header Out[4]: ['index', 0, 1, 2, 3] In [8]: table=[('Alice',1,2,3,4),('Bob',2,3,4,5)] # 表格內容的定義 In [9]: table Out[9]: [('Alice', 1, 2, 3, 4), ('Bob', 2, 3, 4, 5)] In [11]: print(tabulate(table,headers=header,tablefmt='grid')) # 用grid的格式打印表格內容 +---------+-----+-----+-----+-----+ | index | 0 | 1 | 2 | 3 | +=========+=====+=====+=====+=====+ | Alice | 1 | 2 | 3 | 4 | +---------+-----+-----+-----+-----+ | Bob | 2 | 3 | 4 | 5 | +---------+-----+-----+-----+-----+ In [12]: print(tabulate(table,headers=header,tablefmt='fancy_grid')) # 用fancy_grid的格式打印 ╒═════════╤═════╤═════╤═════╤═════╕ │ index │ 0 │ 1 │ 2 │ 3 │ ╞═════════╪═════╪═════╪═════╪═════╡ │ Alice │ 1 │ 2 │ 3 │ 4 │ ├─────────┼─────┼─────┼─────┼─────┤ │ Bob │ 2 │ 3 │ 4 │ 5 │ ╘═════════╧═════╧═════╧═════╧═════╛
在這個案例中,我們分別產(chǎn)生了數(shù)組格式的表頭和表格內容,然后用tabulate進行封裝之后再打印出來。由于tabulate
支持多種格式的輸出,這里我們展示的僅有grid
和fancy_grid
兩種個人比較喜歡的格式。其他類型的格式還有:
"plain" "simple" "github" "grid" "fancy_grid" "pipe" "orgtbl" "jira" "presto" "psql" "rst" "mediawiki" "moinmoin" "youtrack" "html" "latex" "latex_raw" "latex_booktabs" "textile"
使用prettytable美化輸出
類似于tabulate的,prettytable的主要目的也是規(guī)范化的美化表格數(shù)據(jù)的輸出,但是在使用方法上略有差異,在不同的場景下可以使用不同的方案。這里我們先看一下prettytable的安裝,同樣可以使用pip來進行管理:
[dechin@dechin-manjaro table]$ python3 -m pip install prettytable Collecting prettytable Downloading prettytable-2.1.0-py3-none-any.whl (22 kB) Requirement already satisfied: wcwidth in /home/dechin/anaconda3/lib/python3.8/site-packages (from prettytable) (0.2.5) Installing collected packages: prettytable Successfully installed prettytable-2.1.0
安裝完成后我們用一個py文件的示例來展示其用法:
# pt_test.py from prettytable import PrettyTable tb = PrettyTable() # 生成表格對象 tb.field_names = ['Index', 0, 1, 2, 3] # 定義表頭 tb.add_row(['Alice',1,2,3,4]) # 添加一行,列是column tb.add_row(['Bob',2,3,4,5]) print (tb) # 打印輸出
代碼的執(zhí)行結果如下:
[dechin@dechin-manjaro table]$ python3 pt_test.py +-------+---+---+---+---+ | Index | 0 | 1 | 2 | 3 | +-------+---+---+---+---+ | Alice | 1 | 2 | 3 | 4 | | Bob | 2 | 3 | 4 | 5 | +-------+---+---+---+---+
由于使用的案例跟上面介紹的tabulate是一樣的,所以輸出結果也類似,相當于多了一種輸出格式。但是除了輸出格式之外,我們發(fā)現(xiàn)prettytable可以很好的利用行和列的添加的形式來進行表格操作,操作習慣更接近于數(shù)據(jù)庫的操作形式,因此對于經(jīng)常使用數(shù)據(jù)庫的人而言,prettytable可能是一種更好的表格數(shù)據(jù)輸出解決方案。
總結概要
本文介紹了兩種表格數(shù)據(jù)的打印工具:tabulate和prettytable的安裝與基本使用方法。由于表格數(shù)據(jù)本身是沒有對輸出格式進行規(guī)范化的,因此打印出來的數(shù)據(jù)會顯得比較雜亂,不利于直觀的閱讀。因此引入這兩種工具,加強了輸出結果的可讀性。這兩者在使用上各有優(yōu)劣,tabulate支持更多形式的表格樣式,而prettytable則使用了更加接近于數(shù)據(jù)庫的操作形式,對于部分用戶而言有天然的生態(tài)優(yōu)勢。
版權聲明
本文首發(fā)鏈接為:https://www.cnblogs.com/dechinphy/p/table.html
作者ID:DechinPhy
更多原著文章請參考:https://www.cnblogs.com/dechinphy/
參考鏈接https://blog.csdn.net/qq_43901693/article/details/104920856https://blog.csdn.net/u010359398/article/details/82766474
到此這篇關于python3美化表格數(shù)據(jù)輸出結果的文章就介紹到這了,更多相關python表格美化輸出內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python中的response.text與content區(qū)別詳解
這篇文章主要介紹了Python中的response.text與content區(qū)別詳解,?從網(wǎng)絡請求下來的數(shù)據(jù),他們都是字節(jié)類型的,如果服務器不指定的話,默認編碼是"ISO-8859-1",我們使用text直接拿到的是字符串類型,沒有進行解碼操作,則會出現(xiàn)亂碼問題,需要的朋友可以參考下2023-12-12使用Python實現(xiàn)數(shù)據(jù)重采樣的示例代碼
數(shù)據(jù)重采樣是一種用于調整數(shù)據(jù)集大小或分布的技術,它涉及通過增加或減少數(shù)據(jù)點的數(shù)量來修改現(xiàn)有數(shù)據(jù)集,下面我們就來學習一下如何利用Python實現(xiàn)數(shù)據(jù)重采樣吧2023-11-11使用Python通過win32 COM打開Excel并添加Sheet的方法
今天小編就為大家分享一篇使用Python通過win32 COM打開Excel并添加Sheet的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05