参考链接:http://zetcode.com/python/prettytable/

PrettyTable能在python中生成ASCII 表,可以使用他控制表的很多方面,包括文本对齐、表的边框、列的宽度、对数据进行排序、选择在最后的输出中显示制定的行或者列

支持从CSV、HTML、数据库游标中导入数据,能输出数据到ASCII 或者HTML

安装

pip install pTable

使用

  产生一个表(通过add_row()),不用指定数据类型

from prettytable import PrettyTable

x = PrettyTable()#第一步创建对象

x.field_names = ["City name", "Area", "Population", "Annual Rainfall"]#设置表头名称
#通过 add_row()方法创建一个表
x.add_row(["Adelaide", 1295, 1158259, 600.5])
x.add_row(["Brisbane", 5905, 1857594, 1146.4])
x.add_row(["Darwin", 112, 120900, 1714.7])
x.add_row(["Hobart", 1357, 205556, 619.5])
x.add_row(["Sydney", 2058, 4336374, 1214.8])
x.add_row(["Melbourne", 1566, 3806092, 646.9])
x.add_row(["Perth", 5386, 1554769, 869.4]) print(x)

  

$ ./create_by_row.py
+-----------+------+------------+-----------------+
| 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 |
+-----------+------+------------+-----------------+

  产生一个表(通过add_column())#也是先设置表头名称,然后一下添加一列关于相同的属性的不同的值

#!/usr/bin/python3

from prettytable import PrettyTable

x = PrettyTable()

column_names = ["City name", "Area", "Population", "Annual Rainfall"]

x.add_column(column_names[0], ["Adelaide", "Brisbane", "Darwin",
"Hobart", "Sydney", "Melbourne", "Perth"])
x.add_column(column_names[1], [1295, 5905, 112, 1357, 2058, 1566, 5386 ])
x.add_column(column_names[2], [1158259, 1857594, 120900, 205556, 4336374,
3806092, 1554769])
x.add_column(column_names[3], [600.5, 1146.4, 1714.7, 619.5, 1214.8,
646.9, 869.4]) print(x)

  删除 行

  del_row()通过从零开始的索引来删除指定行

#!/usr/bin/python3

from prettytable import PrettyTable

x = PrettyTable()

x.field_names = ["City name", "Area", "Population", "Annual Rainfall"]

x.add_row(["Adelaide", 1295, 1158259, 600.5])
x.add_row(["Brisbane", 5905, 1857594, 1146.4])
x.add_row(["Darwin", 112, 120900, 1714.7])
x.add_row(["Hobart", 1357, 205556, 619.5])
x.add_row(["Sydney", 2058, 4336374, 1214.8])
x.add_row(["Melbourne", 1566, 3806092, 646.9])
x.add_row(["Perth", 5386, 1554769, 869.4]) x.del_row(6)
x.del_row(5)
x.del_row(4)
x.del_row(3) print(x)
#结果:
$ ./delete_rows.py
+-----------+------+------------+-----------------+
| City name | Area | Population | Annual Rainfall |
+-----------+------+------------+-----------------+
| Adelaide | 1295 | 1158259 | 600.5 |
| Brisbane | 5905 | 1857594 | 1146.4 |
| Darwin | 112 | 120900 | 1714.7 |
+-----------+------+------------+-----------------+

  清空数据:

  clear_rows()清楚所有的行但是会保留当前表的列名称,而clear()清除所有的包括行和列名

#!/usr/bin/python3

from prettytable import PrettyTable

x = PrettyTable()

x.field_names = ["City name", "Area", "Population", "Annual Rainfall"]

x.add_row(["Adelaide", 1295, 1158259, 600.5])
x.add_row(["Brisbane", 5905, 1857594, 1146.4])
x.add_row(["Darwin", 112, 120900, 1714.7])
x.add_row(["Hobart", 1357, 205556, 619.5])
x.add_row(["Sydney", 2058, 4336374, 1214.8])
x.add_row(["Melbourne", 1566, 3806092, 646.9])
x.add_row(["Perth", 5386, 1554769, 869.4]) x.clear_rows()
print(x)
#输出
$ ./clear_rows.py
+-----------+------+------------+-----------------+
| City name | Area | Population | Annual Rainfall |
+-----------+------+------------+-----------------+
+-----------+------+------------+-----------------+

  对数据进行排序

  通过sortby属性,我们可以对指定的进行排序,reversesort属性控制排序的方向(升序或者降序)

#!/usr/bin/python3

from prettytable import PrettyTable

x = PrettyTable()
x.field_names = ["City name", "Area", "Population", "Annual Rainfall"] x.add_row(["Adelaide", 1295, 1158259, 600.5])
x.add_row(["Brisbane", 5905, 1857594, 1146.4])
x.add_row(["Darwin", 112, 120900, 1714.7])
x.add_row(["Hobart", 1357, 205556, 619.5])
x.add_row(["Sydney", 2058, 4336374, 1214.8])
x.add_row(["Melbourne", 1566, 3806092, 646.9])
x.add_row(["Perth", 5386, 1554769, 869.4]) print("Table sorted by population:")
x.sortby = "Population"
print(x) print() print("Table sorted by city in descendig order:")
x.sortby = "City name"
x.reversesort = True
print(x)
#第一次的输出:
$ ./sorting.py
Table sorted by population:
+-----------+------+------------+-----------------+
| City name | Area | Population | Annual Rainfall |
+-----------+------+------------+-----------------+
| Darwin | 112 | 120900 | 1714.7 |
| Hobart | 1357 | 205556 | 619.5 |
| Adelaide | 1295 | 1158259 | 600.5 |
| Perth | 5386 | 1554769 | 869.4 |
| Brisbane | 5905 | 1857594 | 1146.4 |
| Melbourne | 1566 | 3806092 | 646.9 |
| Sydney | 2058 | 4336374 | 1214.8 |
+-----------+------+------------+-----------------+
#第二次的输出
Table sorted by city in descendig order:
+-----------+------+------------+-----------------+
| City name | Area | Population | Annual Rainfall |
+-----------+------+------------+-----------------+
| Sydney | 2058 | 4336374 | 1214.8 |
| Perth | 5386 | 1554769 | 869.4 |
| Melbourne | 1566 | 3806092 | 646.9 |
| Hobart | 1357 | 205556 | 619.5 |
| Darwin | 112 | 120900 | 1714.7 |
| Brisbane | 5905 | 1857594 | 1146.4 |
| Adelaide | 1295 | 1158259 | 600.5 |
+-----------+------+------------+-----------------+

  对齐数据

  align属性控制,取值为:l、c、r

#!/usr/bin/python3

from prettytable import PrettyTable

x = PrettyTable()

x.field_names = ["City name", "Area", "Population", "Annual Rainfall"]

x.align["City name"] = "l"#如果不指示某些特定的列就对所有的如x.align='l'#将会对所有的行
x.align["Area"] = "r"
x.align["Annual Rainfall"] = "r" x.add_row(["Adelaide", 1295, 1158259, 600.5])
x.add_row(["Brisbane", 5905, 1857594, 1146.4])
x.add_row(["Darwin", 112, 120900, 1714.7])
x.add_row(["Hobart", 1357, 205556, 619.5])
x.add_row(["Sydney", 2058, 4336374, 1214.8])
x.add_row(["Melbourne", 1566, 3806092, 646.9])
x.add_row(["Perth", 5386, 1554769, 869.4]) print(x)
#输出
$ ./alignment.py
+-----------+------+------------+-----------------+
| 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 |
+-----------+------+------------+-----------------+

  

get_string()方法、控制表显示什么

  这个方法返回 里面的参数对当前表的影响,他有几个选项来控制table怎样显示

  title参数为输出的表加上名字

---snip---
print(x.get_string(title="Australian cities"))#就是在print里面 #输出
$ ./table_title.py
+-------------------------------------------------+
| Australian cities |
+-----------+------+------------+-----------------+
| 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 |
+-----------+------+------------+-----------------+

  fields参数能选择那些列将要被显示出来

---snip---
print(x.get_string(fields=["City name", "Population"]))
#输出
$ ./select_columns.py
+-----------+------------+
| City name | Population |
+-----------+------------+
| Adelaide | 1158259 |
| Brisbane | 1857594 |
| Darwin | 120900 |
| Hobart | 205556 |
| Sydney | 4336374 |
| Melbourne | 3806092 |
| Perth | 1554769 |
+-----------+------------+

  

  通过start和end参数,我们能选择那些行将要被显示出来

---snip---
print(x.get_string(start=1, end=4))
#输出
$ ./select_rows.py
+-----------+------+------------+-----------------+
| City name | Area | Population | Annual Rainfall |
+-----------+------+------------+-----------------+
| Brisbane | 5905 | 1857594 | 1146.4 |
| Darwin | 112 | 120900 | 1714.7 |
| Hobart | 1357 | 205556 | 619.5 |
+-----------+------+------------+-----------------+

  PrettyTable还可以通过CSV、database cursor HTML 导入数据、输出数据到HTMl,参见:http://zetcode.com/python/prettytable/

  

错误

  *)TypeError: add_row() missing 1 required positional argument: 'row'

(sort) λ python some_sort.py
Traceback (most recent call last):
File "some_sort.py", line 1327, in <module>
compared_all(collection,*func_list)
File "some_sort.py", line 1288, in compared_all
x.add_row(['1','2'])
TypeError: add_row() missing 1 required positional argument: 'row'

  因为在创建对象的时候的语句写错了

    x=PrettyTable#这里写错了
x=PrettyTable()

  

  

  

python 库 PrettyTabble 使用与错误的更多相关文章

  1. 11个并不广为人知,但值得了解的Python库

    这是一篇译文,文中提及了一些不常见但是有用的Python库 原文地址:http://blog.yhathq.com/posts/11-python-libraries-you-might-not-kn ...

  2. Windows版的各种Python库安装包下载地址与安装过程

    在用Python开发时(Windows环境),会碰到需要安装某个版本的第三方库,为了以后查找.安装方便,总结如下: windows版的各种Python库安装包下载地址:http://www.lfd.u ...

  3. Python 库大全

    作者:Lingfeng Ai链接:http://www.zhihu.com/question/24590883/answer/92420471来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非 ...

  4. python 库安装笔记

    python 库安装笔记 zoerywzhou@163.com http://www.cnblogs.com/swje/ 作者:Zhouwan 2017-2-22 友情提示 安装python库的过程中 ...

  5. Python库的安装

    window下python2.python3安装包的方法 一.在线安装 安装好python.设置好环境变量后,在python安装目录下Script文件夹内会存在pip.exe和easy_install ...

  6. 哪些 Python 库让你相见恨晚?【转】

    原文链接:https://www.zhihu.com/question/24590883/answer/92420471 原文链接:Python 资源大全 ---------------- 这又是一个 ...

  7. Python常见十六个错误集合,你知道那些?

    使用python会出现各种各样的错误,以下是Python常见的错误以及解决方法. 1.ValueError: 'Conv2d_1a_3×3' is not a valid scope name 这个是 ...

  8. Python库,让你相见恨晚的第三方库

    环境管理 管理 Python 版本和环境的工具 p – 非常简单的交互式 python 版本管理工具.pyenv – 简单的 Python 版本管理工具.Vex – 可以在虚拟环境中执行命令.virt ...

  9. Python 库,资源

    库名称简介 Chardet字符编码探测器,可以自动检测文本.网页.xml的编码. colorama主要用来给文本添加各种颜色,并且非常简单易用. Prettytable主要用于在终端或浏览器端构建格式 ...

随机推荐

  1. Django RestFramework(DRF)类视图

    基础视图 1.基础函数视图(@api_view) DRF提供了一种函数基础视图来装饰Django的普通视图,我们可以使用request来接受请求和response响应.一个小例子: from rest ...

  2. 其它综合-CentOS7 解决忘记root密码

    CentOS7 解决忘记root密码 1.重启 长时间不用的 CentOS 机器再次开机的时候忽然忘记了密码,总不能就重装一台吧,还有好多服务在机器上,于是决定重置 root 的密码. 如果是已经开启 ...

  3. Pytorch中Module,Parameter和Buffer的区别

    下文都将torch.nn简写成nn Module: 就是我们常用的torch.nn.Module类,你定义的所有网络结构都必须继承这个类. Buffer: buffer和parameter相对,就是指 ...

  4. Hello,DTOS!(下)

    如何验证编写的主引导程序?解决方案设计:将汇编源码编译为二进制机器码(nasm)创建虚拟盘(bximage)将二进制代码写入虚拟盘起始位置(dd)在虚拟机中将虚拟盘作为启动盘执行(vmware) 就算 ...

  5. Linux命令——trap

    简介 trap是shell内置命令,它对硬件信号和其他事件做出响应.trap定义并激活信号处理过程,信号处理过程是当shell接收信号或其他特殊条件时要运行的处理过程. 语法 trap [-lp] [ ...

  6. openssl+vsftpd 加密验证方式

    [root@localhost ~]# rpm -q opensslopenssl-1.0.1e-48.el6.x86_64[root@localhost ~]# ldd /usr/sbin/vsft ...

  7. socket服务

    1.socket_server import socket import threading server = socket.socket(socket.AF_INET, socket.SOCK_ST ...

  8. SysML——CSE 599W: Systems for ML

    CSE 599W: Systems for ML Assignments Materials Projects Schedule Schedule The schedule is tentative ...

  9. [BZOJ1864][CODEVS2462]三色二叉树

    题目描述 Description 一棵二叉树可以按照如下规则表示成一个由0.1.2组成的字符序列,我们称之为“二叉树序列S”: |-0  表示该树没有子节点 S = |-1S1 表示该树有一个子节点, ...

  10. LeetCode 1257. Smallest Common Region

    原题链接在这里:https://leetcode.com/problems/smallest-common-region/ 题目: You are given some lists of region ...