简介
Python通过prettytable模块将输出内容如表格方式整齐输出,python本身并不内置,需要独立安装该第三方库。
安装
1 pip install PrettyTable
1 #源码安装 2 wget https://pypi.python.org/packages/source/P/PrettyTable/prettytable-0.7.2.tar.gz 3 tar -zxvf prettytable-0.7.2.tar.gz 4 python setup.py build 5 python setup.py install
使用
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #Author: #### 4 5 from prettytable import PrettyTable 6 x = PrettyTable(["姓名", "性别", "年龄", "存款"]) 7 8 x.align["姓名"] = "1" #以姓名字段左对齐 9 x.padding_width = 1 # 填充宽度 10 x.add_row(["赵一","男", 20, 100000]) 11 x.add_row(["钱二","男", 21, 500]) 12 x.add_row(["孙三", "男", 22, 400.7]) 13 x.add_row(["李四", "男", 23, 619.5]) 14 x.add_row(["周五", "男", 24, 1214.8]) 15 x.add_row(["吴六", "女", 25, 646.9]) 16 x.add_row(["郑七", "女", 26, 869.4]) 17 x.add_row(["王七加一", "男", 21, 869.4]) 18 19 print(x)