Implementation code of python3 beautifying table data output result

  • 2021-10-25 07:29:23
  • OfStack

Technical background

In the previous blog, we introduced the table data processing scheme of python, which focuses on combing, calculating and displaying the table data. This paper focuses on 展示 Work in this area. First, let's look at a case and define an array of tabular data:


[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)]

When we print this table data directly, we find that the effect is very ugly. Although we can get the same information from this table, this method of data presentation is very unfavorable for us to get the data directly from the printout.

Use tabulate to beautify table output

First, a tool tabulate is introduced, which can directly print table data in array format and has a variety of output formats to choose from. The installation method can also be managed with 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)

Installation is easy and there are no other dependencies. Next, we use ipython to show one basic usage:


[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)) #  Definition of table header 

In [4]: header
Out[4]: ['index', 0, 1, 2, 3]

In [8]: table=[('Alice',1,2,3,4),('Bob',2,3,4,5)] #  Definition of table content 

In [9]: table
Out[9]: [('Alice', 1, 2, 3, 4), ('Bob', 2, 3, 4, 5)]

In [11]: print(tabulate(table,headers=header,tablefmt='grid')) #  Use grid Print the contents of the table in the format of 
+---------+-----+-----+-----+-----+
| 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')) #  Use fancy_grid Print in the format of 
 In fact, in fact, the 
 The  index    The    0  The    1  The    2  The    3  The 
 In fact, in fact, the 
 The  Alice    The    1  The    2  The    3  The    4  The 
 Tree-Tree-Tree-Tree-Tree-Tree-Tree-Tree-Tree-Tree-Tree-Tree-Tree-Tree-Tree-Tree-Tree- 
 The  Bob      The    2  The    3  The    4  The    5  The 
 In fact, in fact, the 

In this case, we generated the header and table contents in array format, and then encapsulated them in tabulate before printing them out. Due to tabulate Supports output in multiple formats, and here we show only grid And fancy_grid Two formats that individuals prefer. Other types of formats include:


"plain"
"simple"
"github"
"grid"
"fancy_grid"
"pipe"
"orgtbl"
"jira"
"presto"
"psql"
"rst"
"mediawiki"
"moinmoin"
"youtrack"
"html"
"latex"
"latex_raw"
"latex_booktabs"
"textile"

Beautify the output with prettytable

Similar to tabulate, the main purpose of prettytable is to standardize and beautify the output of tabular data, but there are slight differences in usage methods, and different schemes can be used in different scenarios. Here we first look at the installation of prettytable, which can also be managed by 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

After installation, we use an example of py file to show its usage:


# pt_test.py

from prettytable import PrettyTable

tb = PrettyTable() #  Generate a table object 
tb.field_names = ['Index', 0, 1, 2, 3] #  Define the header 
tb.add_row(['Alice',1,2,3,4]) #  Add 1 Rows and columns are column
tb.add_row(['Bob',2,3,4,5])

print (tb) #  Printout 

The execution result of the code is as follows:


[dechin@dechin-manjaro table]$ python3 pt_test.py 
+-------+---+---+---+---+
| Index | 0 | 1 | 2 | 3 |
+-------+---+---+---+---+
| Alice | 1 | 2 | 3 | 4 |
|  Bob  | 2 | 3 | 4 | 5 |
+-------+---+---+---+---+

Because the case used is the same as the tabulate described above, the output result is similar, which is equivalent to one more output format. However, in addition to the output format, we find that prettytable can make good use of the addition of rows and columns to perform table operation, and the operation habit is closer to the operation form of database. Therefore, for those who often use database, prettytable may be a better table data output solution.

Summary summary

This paper introduces the installation and basic usage of two kinds of tabular data printing tools: tabulate and prettytable. Because the table data itself does not standardize the output format, the printed data will appear messy, which is not conducive to intuitive reading. Therefore, these two tools are introduced to enhance the readability of the output results. Both of them have their own advantages and disadvantages in use. tabulate supports more forms of table styles, while prettytable uses an operation form closer to the database, which has natural ecological advantages for some users.

Copyright notice

The first link of this article is: https://www.cnblogs.com/dechinphy/p/table.html
Author ID: DechinPhy
For more original articles, please refer to: https://www.cnblogs.com/dechinphy/

Reference link https://blog.csdn.net/qq_43901693/article/details/104920856https://blog.csdn.net/u010359398/article/details/82766474


Related articles: