Python prettytable, pythonprettytable

Source: Internet
Author: User

Python prettytable, pythonprettytable

 

>>> From prettytable import PrettyTable >>> x = PrettyTable (["City name", "Area", "Population", "Annual Rainfall"]) >>> x <prettytable. prettyTable object at most >>>> print x + ----------- + ------ + ------------ + province + | City name | Area | Population | Annual Rainfall | + ----------- + ------ + ------------ + ----------------- ++ ----------- + ------ + ------------ + ----------------- +> x. align ['Cit Y 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 (["adelide", 1295,115 600.5,]) >>> print (x) + ----------- + ------ + ------------ + --------------- + | City name | Area | Population | Annual Rainfall | + ----------- + ------ + ---------- + bytes + | adelide | 1295 | 1158259 | 600.5 | + ----------- + ------ + ------------ + ----------------- + >>> # x. a Lign = ['l', 'R', 'C'] left right center # x. padding_width = [width] >>> x = PrettyTable () >>> x. add_column ("City name", ["adelide", "Brisbane", "Darwin", "Hobart", "sysydney", "Melbourne", "Perth"]) >>> x. add_column ("Area", [1295,590 5, 112,135 7, 2058,156 6, 5386]) >>> x. add_column ("Population", [1158259,185 7594, 120900,205 556, 4336374,380 6092, 1554769]) >>> 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 | + ----------- + ------ + ---------- + bytes + | adelide | 1295 | 1158259 | 600.5 | Brisbane | 5905 | 1857594 | 1146.4 | Darwin | 112 | 120900 | 1714.7 | Hobart | 1357 | 205556 | 619.5 | sysydney | 2058 | 4336374 | 1214.8 | Melbourne | 1566 | 38060 92 | 646.9 | Perth | 5386 | 1554769 | 869.4 | + ----------- + ------ + ------------ + ----------------- +> OS. system ('cat ~ /Cj.csv ') language, mathematics, English 121110,125,927, >>>> from prettytable import from_csv >>>> fp = open ("cj.csv", "r") >>> pt = from_csv (fp) >>> fp. close () >>> print pt + ------ + | language | mathematics | English | + ------ + | 133 | 98 | 121 | 110 | 125 | 92 | 73 | 131 | 84 | + ------ +> x. get_string (fields = ["City name", "Population"]) u' + province + ---------------------------- + \ n | City name | Population | \ n + province + \ n | adelide | 1158259 | \ n | Brisbane | 1857594 | \ n | Darwin | 120900 | \ n | Hobart | 205556 | \ n | sysydney | 4336374 | \ n | Melbourne | 3806092 | \ n | Perth | 1554769 | \ n + ------------------------- + ------------------ + '> >>>>>>>>>>> x. get_string (fields = ["City name", "Population"]) u' + province + ---------------------------- + \ n | City name | Population | \ n + province + \ n | adelide | 1158259 | \ n | Brisbane | 1857594 | \ n | Darwin | 120900 | \ n | Hobart | 205556 | \ n | sysydney | 4336374 | \ n | Melbourne | 3806092 | \ n | Perth | 1554769 | \ n + ------------------------- + ------------------ + '>> print x. get_string (start = 0, end = 3) + region + ---------------------- + region + | City name | Area | Population | Annual Rainfall | + region + ---------------------- + region + | adelide | 1295 | 1158259 | 600.5 | Brisbane | 5905 | 1857594 | 1146.4 | Darwin | 112 | 120900 | + ------------------------------- + ---------------------------- + signature +> print x. get_string (sortby = "Annual Rainfall ") + region + ---------------------- + region + | City name | Area | Population | Annual Rainfall | + region + ---------------------- + region + | adelide | 1295 | 1158259 | 600.5 | Hobart | 1357 | 205556 | 619.5 | Melbourne | 1566 | 3806092 | 646.9 | Perth | 5386 | 1554769 | 869.4 | Brisbane | 5905 | 1857594 | 1146.4 | sysydney | 2058 | 4336374 | 1214.8 | Darwin | 112 | 120900 | + ------------------------------- + ------------------------ + ---------------------------- + signature +> print x. get_string (sortby = "Annual Rainfall", reversesort = True) + region + ---------------------- + region + | City name | Area | Population | Annual Rainfall | + region + ---------------------- + region + | Darwin | 112 | 120900 | 1714.7 | sysydney | 2058 | 4336374 | 1214.8 | Brisbane | 5905 | 1857594 | 1146.4 | Perth | 5386 | 1554769 | 869.4 | Melbourne | 1566 | 3806092 | 646.9 | Hobart | 1357 | 205556 | 619.5 | adelide | 1295 | 1158259 | 600.5 | + bytes + -------------------------- + ------------------------------ + bytes + >>> from prettytable import MSWORD_FRIENDLY> x. set_style (MSWORD_FRIENDLY) >>> print (x) | City name | Area | Population | Annual Rainfall | adelide | 1295 | 1158259 | 600.5 | Brisbane | 5905 | 1857594 | 1146.4 | Darwin | 112 | 120900 | Hobart | 1357 | 205556 | 619.5 | sysydney | 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 are displayed. you have the freedom to set each of these options individually to whatever you prefer. the set_style method just does this automatically for you. the options are these: border-A boolean option (must be True or False ). controls whether or not a border is drawn around the table. header-A boolean option (must be True or False ). controls whether or not the first row of the table is a header showing the names of all the fields. header_style-Controls capitalization of field names in the header. allowed values: "cap" (capitalize first letter of each word), "title" (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 are variables defined inside the prettytable module so make sure you import 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" (center), "r" (right )) valign-Vertical alignment (None, "t" (top), "m" (middle) or "B" (bottom) int_format-Controls formatting of integer data. this shoshould be a string which can be placed between "%" and "d" in something like print "% d" % 42. float_format-Controls formatting of floating point data. this shoshould be 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 are None ). left_padding_width-Number of spaces on left hand side of column data. right_padding_width-Number of spaces on right hand side of column data. vertical_char-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 +. ========================================================== ============================

  

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.