Python3之PrettyTable模块
一. 简介
Python通过prettytable模块将输出内容如表格方式整齐输出,python本身并不内置,需要独立安装该第三方库。
二. 安装
方式一:pip安装
1
|
>>> pip install PrettyTable
|
方式二:源码安装
1
2
3
4
|
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
|
三. 使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
>>>
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
|
+
-
-
-
-
-
-
-
+
-
-
-
-
-
+
-
-
-
-
-
+
-
-
-
-
-
-
-
+
|