A detailed description of the Python Practical Library learning Prettytable

Source: Internet
Author: User

Python Utility library: prettytable Learning

Prettytable description

Prettytable is a third-party library in Python that can be used to generate beautiful, ASCII-formatted tables that are useful.
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 is inspired by the ASCII tables used in the PostgreSQL shell psql. Prettytable allows for selection of which columns is to being printed, independent alignment of columns (left or right justi Fied or centred) and printing of "sub-tables" by specifying a row range.

Prettytable installation

The prettytable can be easily installed using PIP, as follows:

Pip Install prettytable

Prettytable Use Example

There are instructions for using prettytable on GitHub, and the links are as follows: Https://github.com/dprince/python-prettytable

The following are specific examples of use:

Import prettytable as pt## add data by Row TB = pt. Prettytable () Tb.field_names = ["City name", "area", "Population", "annual Rainfall"]tb.add_row (["Adelaide", 1295, 1158259, 600.5]) tb.add_row (["Brisbane", 5905, 1857594, 1146.4]) tb.add_row (["Darwin", the 120900, the 1714.7]) Tb.add_row ( ["Hobart", 1357, 205556,619.5]) Print (TB)
+-----------+------+------------+-----------------+| City Name | Area | Population | Annual rainfall |+-----------+------+------------+-----------------+|  Adelaide | 1295 |  1158259   |      600.5      | |  Brisbane | 5905 |  1857594   |      1146.4     | |   Darwin  |   120900   |      1714.7     | |   Hobart  | 1357 |   205556   |      619.5      |+-----------+------+------------+-----------------+
# # Add data by Column Tb.add_column (' Index ', [1,2,3,4]) print (TB)
+-----------+------+------------+-----------------+-------+| City Name | Area | Population | Annual Rainfall | Index |+-----------+------+------------+-----------------+-------+|  Adelaide | 1295 |  1158259   |      600.5      |   1   | |  Brisbane | 5905 |  1857594   |      1146.4     |   2   | |   Darwin  |   120900   |      1714.7     |   3   | |   Hobart  | 1357 |   205556   |      619.5      |   4   |+-----------+------+------------+-----------------+-------+
# # uses a different output style 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, each different Tb.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 120900 1714.7 Hobart 1357 205556 619.5---style:msword_friendly-----@ Adelaide 1295 1158259 600.5 @@ Brisbane 59 1857594 1146.4@@ Darwin 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, get table string s = tb.get_string () print (s) # # can only get the specified column or row s = tb.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  |   120900   |      1714.7     | |   Hobart  | 1357 |   205556   |      619.5      |+-----------+------+------------+-----------------+ +-----------+------------+| City Name | Population |+-----------+------------+|  Brisbane |  1857594   | |   Darwin  |   120900   | |   Hobart  |   205556   |+-----------+------------+
# # Custom table Output Style # # # Set left-justified tb.align = ' L ' # # # # # # # # Set the digital output format Tb.float_format = "2.2" # # # Set Border connector to ' * ' Tb.junction_char = ' * ' # # # # Set Sorting method Tb.s Ortby = "city Name" # # # setting left blank character not filled tb.left_padding_width = 0print (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          |*----------*-----*-----------*----------------*
# # does not show borders Tb.border = 0print (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  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    |  120900 |     1714.70 |         | Hobart |    1357 | 205556     | 619.50          |+++++++++++++++++++++++++++++++++++++++++++++++++++
# # Prettytable also supports output HTML code s = tb.get_html_string () print (s)
<table> <tr> <th>city name</th> <th>Ar ea</th> <th>Population</th> <th>annual rainfall</th> </tr> <tr&        Gt <td>Adelaide</td> <td>1295</td> <td>1158259</td> <td>600.50 </td> </tr> <tr> <td>Brisbane</td> <td>5905</td> < td>1857594</td> <td>1146.40</td> </tr> <tr> <TD>DARWIN</TD&G        T <td>112</td> <td>120900</td> <td>1714.70</td> </tr> <TR&G        T <td>Hobart</td> <td>1357</td> <td>205556</td> <td>619.50< ;/td> </tr></table> 
## use the Copy method to copy the Object #tb.set_style (Pt. DEFAULT) Tb.horizontal_char = '. '  TB2 = Tb.copy () tb.align = ' l ' tb2.align = ' r ' Print (TB) print (TB2) # # Direct assignment, get index Tb.horizontal_char = '-' tb.aliign = ' l ' tb3 = Tbtb3.align = ' R ' Print (TB) print (TB3) 
+...........+......+............+.................+| 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 |+-----------+------+------------+-----------------+
Related Article

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.