A detailed description of learning PrettyTable using the python utility library

Source: Internet
Author: User
This article introduces how to learn PrettyTable from the python utility library. a detailed description of python utility Library: PrettyTable learning PrettyTable

PrettyTable is a third-party library in python. it can be used to generate beautiful ASCII tables and is very practical.
The following is an official introduction:

A simple Python library for easily displaying tabular data in a visually appealing ASCII table format.
PrettyTable is a simple Python library designed to make it quick and easy to represent tabular data in visually appealing ASCII tables. it was should red by the ASCII tables used in the PostgreSQL shell psql. prettyTable allows for selection of which columns are to be printed, independent alignment of columns (left or right justified or centred) and printing of "sub-tables" by specifying a row range.

PrettyTable installation

You can easily install PrettyTable using pip, as shown below:

pip install PrettyTable
PrettyTable example

Github has PrettyTable instructions, links are as follows: https://github.com/dprince/python-prettytable

The following is an example:

Import prettytable as pt # Add data tb = pt by row. prettyTable () tb. field_names = ["City name", "Area", "Population", "Annual Rainfall"] tb. add_row (["Adelide", 1295,115 600.5,]) tb. add_row (["Brisbane", 5905,185 7594, 1146.4]) tb. add_row (["Darwin", 112,120 900, 1714.7]) tb. add_row (["Hobart", 1357,205 556,619.5]) print (tb)
+-----------+------+------------+-----------------+| City name | Area | Population | Annual Rainfall |+-----------+------+------------+-----------------+|  Adelaide | 1295 |  1158259   |      600.5      ||  Brisbane | 5905 |  1857594   |      1146.4     ||   Darwin  | 112  |   120900   |      1714.7     ||   Hobart  | 1357 |   205556   |      619.5      |+-----------+------+------------+-----------------+
# Add data by column tb. add_column ('index', [1, 2, 4]) print (tb)
+-----------+------+------------+-----------------+-------+| City name | Area | Population | Annual Rainfall | index |+-----------+------+------------+-----------------+-------+|  Adelaide | 1295 |  1158259   |      600.5      |   1   ||  Brisbane | 5905 |  1857594   |      1146.4     |   2   ||   Darwin  | 112  |   120900   |      1714.7     |   3   ||   Hobart  | 1357 |   205556   |      619.5      |   4   |+-----------+------+------------+-----------------+-------+
# Use different output styles tb. set_style (pt. MSWORD_FRIENDLY) print ('--- style: MSWORD_FRIENDLY -----') print (tb) tb. set_style (pt. PLAIN_COLUMNS) print ('--- style: PLAIN_COLUMNS -----') print (tb) # Random style, different tb each time. set_style (pt. RANDOM) print ('--- style: MSWORD_FRIENDLY -----') print (tb) tb. set_style (pt. DEFAULT) print ('--- style: DEFAULT -----') print (tb)
--- style:MSWORD_FRIENDLY -----| City name | Area | Population | Annual Rainfall ||  Adelaide | 1295 |  1158259   |      600.5      ||  Brisbane | 5905 |  1857594   |      1146.4     ||   Darwin  | 112  |   120900   |      1714.7     ||   Hobart  | 1357 |   205556   |      619.5      |--- style:PLAIN_COLUMNS -----City name        Area        Population        Annual Rainfall         Adelaide        1295         1158259               600.5              Brisbane        5905         1857594               1146.4              Darwin         112           120900               1714.7              Hobart         1357          205556               619.5             --- style:MSWORD_FRIENDLY -----@    Adelaide     1295     1158259     600.5 @@    Brisbane     5905     1857594     1146.4@@     Darwin      112       120900     1714.7@@     Hobart      1357      205556     619.5 @--- style:DEFAULT -----+-----------+------+------------+-----------------+| City name | Area | Population | Annual Rainfall |+-----------+------+------------+-----------------+|  Adelaide | 1295 |  1158259   |      600.5      ||  Brisbane | 5905 |  1857594   |      1146.4     ||   Darwin  | 112  |   120900   |      1714.7     ||   Hobart  | 1357 |   205556   |      619.5      |+-----------+------+------------+-----------------+
# Do not print. obtain the table string s = tb. get_string () print (s) # Only the specified column or row s = tb can be obtained. get_string (fields = ["City name", "Population"], start = 1, end = 4) print (s)
+-----------+------+------------+-----------------+| City name | Area | Population | Annual Rainfall |+-----------+------+------------+-----------------+|  Adelaide | 1295 |  1158259   |      600.5      ||  Brisbane | 5905 |  1857594   |      1146.4     ||   Darwin  | 112  |   120900   |      1714.7     ||   Hobart  | 1357 |   205556   |      619.5      |+-----------+------+------------+-----------------++-----------+------------+| City name | Population |+-----------+------------+|  Brisbane |  1857594   ||   Darwin  |   120900   ||   Hobart  |   205556   |+-----------+------------+
# Customize the output style of a table ### set the left-aligned tb. align = 'l' ### set the digital output format to tb. float_format = "2.2" ### set the border connector to '* "tb. junction_char = "*" ### set the sorting method tb. sortby = "City name" ### set the left side to not fill in the space tb. left_padding_width = 0 print (tb)
*----------*-----*-----------*----------------*|City name |Area |Population |Annual Rainfall |*----------*-----*-----------*----------------*|Adelaide  |1295 |1158259    |600.50          ||Brisbane  |5905 |1857594    |1146.40         ||Darwin    |112  |120900     |1714.70         ||Hobart    |1357 |205556     |619.50          |*----------*-----*-----------*----------------*
# Do not display the border tb. border = 0 print (tb) # Modify the border separator tb. set_style (pt. DEFAULT) tb. horizontal_char = '+ 'print (tb)
City name Area Population Annual Rainfall Adelaide  1295 1158259    600.50          Brisbane  5905 1857594    1146.40         Darwin    112  120900     1714.70         Hobart    1357 205556     619.50          +++++++++++++++++++++++++++++++++++++++++++++++++++| City name | Area | Population | Annual Rainfall |+++++++++++++++++++++++++++++++++++++++++++++++++++| Adelaide  | 1295 | 1158259    | 600.50          || Brisbane  | 5905 | 1857594    | 1146.40         || Darwin    | 112  | 120900     | 1714.70         || Hobart    | 1357 | 205556     | 619.50          |+++++++++++++++++++++++++++++++++++++++++++++++++++
# Prettytable also supports outputting HTML code s = tb. get_html_string () print (s)
 
 
City name Area Population Annual Rainfall
Adelaide 1295 1158259 600.50
Brisbane 5905 1857594 1146.40
Darwin 112 120900 1714.70
Hobart 1357 205556 619.50
# Use the copy method to copy objects # tb. set_style (pt. DEFAULT) tb. horizontal_char = '. 'tb2 = tb. copy () tb. align = 'l' tb2. align = 'R' print (tb) print (tb2) # assign a value directly to obtain the index tb. horizontal_char = '-' tb. aliign = 'l' = tbtb3.align = 'R' print (tb) print)
+...........+......+............+.................+| City name | Area | Population | Annual Rainfall |+...........+......+............+.................+| Adelaide  | 1295 | 1158259    | 600.50          || Brisbane  | 5905 | 1857594    | 1146.40         || Darwin    | 112  | 120900     | 1714.70         || Hobart    | 1357 | 205556     | 619.50          |+...........+......+............+.................++...........+......+............+.................+| City name | Area | Population | Annual Rainfall |+...........+......+............+.................+|  Adelaide | 1295 |    1158259 |          600.50 ||  Brisbane | 5905 |    1857594 |         1146.40 ||    Darwin |  112 |     120900 |         1714.70 ||    Hobart | 1357 |     205556 |          619.50 |+...........+......+............+.................++-----------+------+------------+-----------------+| City name | Area | Population | Annual Rainfall |+-----------+------+------------+-----------------+|  Adelaide | 1295 |    1158259 |          600.50 ||  Brisbane | 5905 |    1857594 |         1146.40 ||    Darwin |  112 |     120900 |         1714.70 ||    Hobart | 1357 |     205556 |          619.50 |+-----------+------+------------+-----------------++-----------+------+------------+-----------------+| City name | Area | Population | Annual Rainfall |+-----------+------+------------+-----------------+|  Adelaide | 1295 |    1158259 |          600.50 ||  Brisbane | 5905 |    1857594 |         1146.40 ||    Darwin |  112 |     120900 |         1714.70 ||    Hobart | 1357 |     205556 |          619.50 |+-----------+------+------------+-----------------+

The above is a detailed description of the python utility library to learn PrettyTable. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.