码迷,mamicode.com
首页 > 编程语言 > 详细

python之prettytable

时间:2016-04-16 08:23:44      阅读:520      评论:0      收藏:0      [点我收藏+]

标签:

 

>>> from prettytable import PrettyTable
>>> x = PrettyTable(["City name", "Area", "Population", "Annual Rainfall"])  
>>> 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 =[宽度]

>>> 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]) 
>>> 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‘)
语文,数学,英语
133,98,121
110,125,92
73,131,84
0
>>> from prettytable import from_csv 
>>> fp = open("cj.csv", "r")
>>> pt = from_csv(fp)
>>> fp.close()
>>> print pt
+------+------+------+
| 语文 | 数学 | 英语 |
+------+------+------+
| 133  |  98  | 121  |
| 110  | 125  |  92  |
|  73  | 131  |  84  |
+------+------+------+
>>> 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+-----------------------------+------------------------------+‘
>>> 
>>> 
>>> 
>>> 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 style

PrettyTable 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 capitalisation of field names in the header. Allowed values: "cap" (capitalise 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, NONE
align - Horizontal alignment (None, "l" (left), "c" (centre), "r" (right))
valign - Vertical alignment (None, "t" (top), "m" (middle) or "b" (bottom))
int_format - Controls formatting of integer data. This should 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 should 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 +.
==========================================================

  

python之prettytable

标签:

原文地址:http://www.cnblogs.com/similarface/p/5397570.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!