Python prettytable模塊


Python  prettytable模塊 

Python通過prettytable模塊將輸出內容

如表格方式整齊輸出:

首先下載prettytable模塊,
下載連接:https://pypi.python.org/pypi/PrettyTable
下載後解壓: 
wget https://pypi.python.org/packages/source/P/PrettyTable/prettytable-0.7.2.tar.gz#md5=a6b80afeef286ce66733d54a0296b13b
tar zxf prettytable-0.7.2.tar.gz 
1:cp prettytable.py  /usr/lib/python2.6/site-packages/
2:chmod a+rx  /usr/lib/python2.6/site-packages/prettytable.py
Python 2.6.6 (r266:84292, Jul 23 2015, 15:22:56) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from prettytable import PrettyTable  
>>> 
>>> x = PrettyTable(["City name", "Area", "Population", "Annual Rainfall"]) 
>>> x.align["City name"] = "l"# Left align city names
>>> x.padding_width = 1# One space between column edges and contents (default)
>>> x.add_row(["Brisbane",5905, 1857594, 1146.4]) 
>>> x.add_row(["Hobart", 1357, 205556, 619.5])  
>>> print x 
+-----------+------+------------+-----------------+
| City name | Area | Population | Annual Rainfall |
+-----------+------+------------+-----------------+
| Brisbane  | 5905 |  1857594   |      1146.4     |
| Hobart    | 1357 |   205556   |      619.5      |
+-----------+------+------------+-----------------+
>>>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章