網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
python可以美化表格數(shù)據(jù)輸出結(jié)果的兩個(gè)工具_(dá)python
作者:Python?學(xué)習(xí)者 ? 更新時(shí)間: 2022-08-23 編程語(yǔ)言前言
在用python處理表格數(shù)據(jù)中,這其中的工作重點(diǎn)就是對(duì)表格類(lèi)型的數(shù)據(jù)進(jìn)行梳理、計(jì)算和展示,本文重點(diǎn)介紹展示這個(gè)方面的工作。
首先我們看一個(gè)案例,定義一個(gè)數(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)]
當(dāng)我們直接打印這個(gè)表格數(shù)據(jù)的時(shí)候,發(fā)現(xiàn)效果非常的難看。雖然我們可以從這個(gè)表格中獲取到同樣的信息,但是這種數(shù)據(jù)展示的方法對(duì)于我們直接從打印輸出中獲取數(shù)據(jù)是非常不利的。
1.使用tabulate美化表格輸出
首先介紹一個(gè)工具tabulate,可以直接打印數(shù)組格式的表格數(shù)據(jù),并且有多種輸出格式可選。安裝方法同樣可以用pip來(lái)進(jìn)行管理:
[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)
安裝很容易,也沒(méi)有其他依賴(lài)。
接下來(lái)我們用ipython來(lái)展示一些基本用法:
[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)] # 表格內(nèi)容的定義
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的格式打印表格內(nèi)容
+---------+-----+-----+-----+-----+
| 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 │
╘═════════╧═════╧═════╧═════╧═════╛
在這個(gè)案例中,我們分別產(chǎn)生了數(shù)組格式的表頭和表格內(nèi)容,然后用tabulate進(jìn)行封裝之后再打印出來(lái)。由于tabulate支持多種格式的輸出,這里我們展示的僅有g(shù)rid和fancy_grid兩種個(gè)人比較喜歡的格式
其他類(lèi)型的格式還有:
"plain"
"simple"
"github"
"grid"
"fancy_grid"
"pipe"
"orgtbl"
"jira"
"presto"
"psql"
"rst"
"mediawiki"
"moinmoin"
"youtrack"
"html"
"latex"
"latex_raw"
"latex_booktabs"
"textile"
2.使用prettytable美化輸出
類(lèi)似于tabulate的,prettytable的主要目的也是規(guī)范化的美化表格數(shù)據(jù)的輸出,但是在使用方法上略有差異,在不同的場(chǎng)景下可以使用不同的方案。
這里我們先看一下prettytable的安裝,同樣可以使用pip來(lái)進(jìn)行管理:
[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
安裝完成后我們用一個(gè)py文件的示例來(lái)展示其用法:
from prettytable import PrettyTable
tb = PrettyTable() # 生成表格對(duì)象
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í)行結(jié)果如下:
[dechin@dechin-manjaro table]$ python3 pt_test.py
+-------+---+---+---+---+
| Index | 0 | 1 | 2 | 3 |
+-------+---+---+---+---+
| Alice | 1 | 2 | 3 | 4 |
| Bob | 2 | 3 | 4 | 5 |
+-------+---+---+---+---+
由于使用的案例跟上面介紹的tabulate是一樣的,所以輸出結(jié)果也類(lèi)似,相當(dāng)于多了一種輸出格式。但是除了輸出格式之外,我們發(fā)現(xiàn)prettytable可以很好的利用行和列的添加的形式來(lái)進(jìn)行表格操作,操作習(xí)慣更接近于數(shù)據(jù)庫(kù)的操作形式,因此對(duì)于經(jīng)常使用數(shù)據(jù)庫(kù)的人而言,prettytable可能是一種更好的表格數(shù)據(jù)輸出解決方案。
總結(jié)
本文介紹了兩種表格數(shù)據(jù)的打印工具:tabulate和prettytable的安裝與基本使用方法。由于表格數(shù)據(jù)本身是沒(méi)有對(duì)輸出格式進(jìn)行規(guī)范化的,因此打印出來(lái)的數(shù)據(jù)會(huì)顯得比較雜亂,不利于直觀的閱讀。因此引入這兩種工具,加強(qiáng)了輸出結(jié)果的可讀性。這兩者在使用上各有優(yōu)劣,tabulate支持更多形式的表格樣式,而prettytable則使用了更加接近于數(shù)據(jù)庫(kù)的操作形式,對(duì)于部分用戶(hù)而言有天然的生態(tài)優(yōu)勢(shì)。
原文鏈接:https://blog.csdn.net/sinat_38682860/article/details/125411333
相關(guān)推薦
- 2022-06-07 python數(shù)組的復(fù)制與列表中的pop_python
- 2023-07-25 SpringBoot配置AOP
- 2023-04-10 詳解Go語(yǔ)言中的數(shù)據(jù)庫(kù)操作_Golang
- 2023-01-23 Linux?paste命令用法匯總_linux shell
- 2022-05-17 SQL?Server實(shí)現(xiàn)分頁(yè)方法介紹_MsSql
- 2022-07-17 使用非root用戶(hù)安裝及啟動(dòng)docker的問(wèn)題(rootless模式運(yùn)行)_docker
- 2022-12-07 org.apache.tomcat.util.http.fileupload.IOUtils報(bào)錯(cuò)對(duì)應(yīng)
- 2022-08-23 構(gòu)建?Python?命令行參數(shù)的?4?種常見(jiàn)方式_python
- 最近更新
-
- 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)證過(guò)濾器
- Spring Security概述快速入門(mén)
- 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)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支