日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(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

欄目分類(lèi)
最近更新