The prettytable of Python

Source: Internet
Author: User

>>> from prettytable import prettytable>>> x = prettytable ([' City name ', ' area ', ' Population ', ' annual R Ainfall "]) >>> x<prettytable. Prettytable object at 0x7f778da86ed0>>>> print x+-----------+------+------------+-----------------+| City Name | Area | Population | Annual rainfall |+-----------+------+------------+-----------------+ +-----------+------+------------+----------- ------+>>> x.align[' city name ', ' C ' >>> x.align[' city name ']= ' 1 ' >>> print x+-----------+--- ---+------------+-----------------+| City Name | Area | Population | Annual rainfall |+-----------+------+------------+-----------------+ +-----------+------+------------+----------- ------+>>> x.padding_width=1>>> Print (x) +-----------+------+------------+-----------------+| City Name | Area | Population | Annual rainfall |+-----------+------+------------+-----------------+ +-----------+------+------------+----------- ------+>>> X.add_rOW (["Adelaide", 1295, 1158259, 600.5]) >>> print (x) +-----------+------+------------+-----------------+| City Name | Area | Population |  Annual rainfall |+-----------+------+------------+-----------------+| Adelaide |  1295 |      1158259 | 600.5 |+-----------+------+------------+-----------------+>>> #x. align= "' l ', ' r ', ' C '" left right center# X.padding_width =[width]>>> x = prettytable () >>> x.add_column ("City Name", ["Adelaide", "Brisbane", " Darwin "," Hobart "," Sydney "," Melbourne "," Perth "]) >>> x.add_column (" area ", [1295, 5905, 112, 1357, 2058, 1566, 5386]) >>> x.add_column ("Population", [1158259, 1857594, 120900, 205556, 4336374, 3806092, 1554769]) >>& Gt X.add_column ("Annual rainfall", [600.5, 1146.4, 1714.7, 619.5, 1214.8, 646.9, 869.4]) >>> print (x) +-----------+ ------+------------+-----------------+| 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 | | Sydney |  2058 |      4336374 | 1214.8 | | Melbourne |  1566 |      3806092 |   646.9 | | Perth |  5386 |      1554769 | 869.4 |+-----------+------+------------+-----------------+>>> os.system (' Cat ~/cj.csv ') Chinese, maths, English 133, 98,121110,125,9273,131,840>>> from prettytable import from_csv >>> fp = open ("Cj.csv", "R") >> > pt = from_csv (fp) >>> fp.close () >>> print pt+------+------+------+| language | Math | English |+------+------+------+|  133 | 98 | 121 | | 110 |  125 |  92 | | 73 |  131 | |+------+------+------+>>> x.get_string (fields=["City Name", "Population"]) u ' +------------------------          -----+------------------------------+\n|          City Name | Population |\n+-----------------------------+------------------------------+\n|           Adelaide |           1158259 |\n|           Brisbane |            1857594 |\n|            Darwin |            120900 |\n|            Hobart |            205556 |\n|           Sydney |          4336374 |\n|           Melbourne |            3806092 |\n|           Perth | 1554769 |\n+-----------------------------+------------------------------+ ' >>> >>> >>&gt ; >>> x.get_string (fields=["City Name", "Population"]) U ' +-----------------------------+-------------------          -----------+\n|          City Name |           Population |\n+-----------------------------+------------------------------+\n|           Adelaide |           1158259 |\n|           Brisbane |            1857594 |\n|            Darwin | 120900 |\n|            Hobart |            205556 |\n|           Sydney |          4336374 |\n|           Melbourne |            3806092 |\n|           Perth | 1554769 |\n+-----------------------------+------------------------------+ ' >>> print x.get_string (start =0,end=3) +-----------------------------+------------------------+------------------------------+--------------          ---------------------+|          City Name |          Area |          Population | Annual rainfall |+-----------------------------+------------------------+------------------------------+--------           ---------------------------+|          Adelaide |           1295 |               1158259 |           600.5 | |          Brisbane |           5905 |               1857594 | 1146.4 ||          Darwin |            112 |               120900 | 1714.7 |+-----------------------------+------------------------+------------------------------+-------------- ---------------------+>>> Print x.get_string (sortby= "annual rainfall") +-----------------------------+---          ---------------------+------------------------------+-----------------------------------+|          City Name |          Area |          Population | Annual rainfall |+-----------------------------+------------------------+------------------------------+--------           ---------------------------+|          Adelaide |           1295 |               1158259 |            600.5 | |          Hobart |            1357 |               205556 |          619.5 | |          Melbourne |           1566 |        3806092    |            646.9 | |          Perth |           5386 |               1554769 |           869.4 | |          Brisbane |           5905 |               1857594 |            1146.4 | |          Sydney |           2058 |               4336374 |            1214.8 | |          Darwin |            112 |               120900 | 1714.7 |+-----------------------------+------------------------+------------------------------+-------------- ---------------------+>>> Print x.get_string (sortby= "annual rainfall", reversesort=true) +----------------          -------------+------------------------+------------------------------+-----------------------------------+|          City Name |          Area |          Population | Annual Rainfall |+-----------------------------+------------------------+------------------------------+-----------------------------------+|          Darwin |            112 |               120900 |            1714.7 | |          Sydney |           2058 |               4336374 |           1214.8 | |          Brisbane |           5905 |               1857594 |            1146.4 | |          Perth |           5386 |               1554769 |          869.4 | |          Melbourne |           1566 |               3806092 |            646.9 | |          Hobart |            1357 |               205556 |           619.5 | |          Adelaide |           1295 |               1158259 | 600.5 |+-----------------------------+------------------------+------------------------------+-----------------------------------+>>> from prettytable import msword_friendly>>> X.set_style (msword_friendly) >>> print (x) | 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 | | Sydney |  2058 |      4336374 | 1214.8 | | Melbourne |  1566 |      3806092 |   646.9 | | Perth |  5386 |      1554769 | 869.4 |========================================================manually Changing table styleprettytable has a number of style options which control various aspects of how tables is displayed. You had the freedom to set each of the these options individually to whatever you prefer. The Set_style method just does this automatically for you. The options is these:border-a boolean option (Must be True oR False). Controls whether or not a border are drawn around the table.header-a boolean option (must be True or False).  Controls whether or not the first row of the table was a header showing the names of all the Fields.header_style-controls Capitalisation of field names in the header.  Allowed values: "Cap" (capitalise first letter of each word), "title" (the title case), "Upper" (All Upper-case), "Lower" (all lower-case) or None (don ' t change from original field name setting). Default is none.hrules-controls printing of horizontal rules after rows. Allowed Values:frame, all, none-note that these is variables defined inside the Prettytable module so do sure you im Port them or use prettytable. FRAME Etc.vrules-controls printing of vertical rules between columns. Allowed Values:frame, all, nonealign-horizontal alignment (None, ' l ' (left), "C" (centre), "R") Valign-vertica L Alignment (None, "T" (top), "M" (middle) or "B" (bottom)) int_format-controls formatting of IntegER data. This should is a string which can be placed between "%" and "D" in something like print "%d"% 42.float_format-controls Formatting of floating point data. This should is a string which can be placed between "%" and "F" in something like print "%f"% 4.2.padding_width-number of spaces on either side of column data (only used if left and right paddings is None). Left_padding_width-number of Spa Ces on left hand side of column data.right_padding_width-number of spaces in right hand side of column Data.vertical_cha R-single character string used to draw vertical lines. Default is |. Horizontal_char-single character string used to draw horizontal lines. Default is-.junction_char-single character string used to draw line junctions. Default is +.==========================================================

  

The prettytable of Python

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.