Python3之PrettyTable模块

一. 简介

  Python通过prettytable模块将输出内容如表格方式整齐输出,python本身并不内置,需要独立安装该第三方库。

二. 安装

方式一:pip安装

>>> pip install PrettyTable

方式二:源码安装

 wget https://pypi.python.org/packages/source/P/PrettyTable/prettytable-0.7.2.tar.gz
tar -zxvf prettytable-0.7.2.tar.gz
python setup.py build
python setup.py install

三. 使用

>>> from prettytable import PrettyTable
>>> x = PrettyTable(["name", "age", "sex", "money"])
>>> x.align["name"] = "l"  # 以name字段左对齐
>>> x.padding_width = 1   # 填充宽度
>>> x.add_row(["wang",20, "man", 1000])
>>> x.add_row(["alex",21, "man", 2000])
>>> x.add_row(["peiqi",22, "man", 3000])
>>> print(x)
+-------+-----+-----+-------+
|  name | age | sex | money |
+-------+-----+-----+-------+
|  wang |  20 | man |  1000 |
|  alex |  21 | man |  2000 |
| peiqi |  22 | man |  3000 |
+-------+-----+-----+-------+

  

  

  

posted @ 2016-07-02 14:10  王永存ღ  阅读(11808)  评论(0编辑  收藏  举报