参考链接: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. testlink 1.9.19安装

    环境平台: 系统:Centos 7.6 数据库:mysql 5.7 PHP版本:PHP 5.6 testlink版本:testlink- 链接:https://pan.baidu.com/s/10Pr ...

  2. 未加载opencv_world330.pdb

    根据设置下载对应的pdb文件. 无法查找或打开pdb文件

  3. 杂项-FLAG

    题目 最低位隐写 50 4B 03 04 压缩格式zip的文件头 save bin 保存成zip格式 解压(WinRAR不能正常解压) 然后用vim打开(winhex也可以) hctf{dd0gf4c ...

  4. python处理JSON 序列化与反序列化

    #序列化 >>> import json>>> d={"key":"value"}>>> d{'key': ...

  5. Django简介(MVC、MTV)

    Django简介 MVC Model(模型)- 应用程序中处理数据逻辑部分且与数据库交互,用于存取数据的部分 View(视图)- 用于处理后的数据界面展示,且视图通常是由模型数据创建的,是用户看到并与 ...

  6. hdfs.server.datanode.DataNode: Block pool ID needed, but service not yet registered with NN

    启动hadoop 发现 50070 的 livenode 数量是 0 查看日志, hdfs.server.datanode.DataNode: Block pool ID needed, but se ...

  7. LG2447/BZOJ1923 「SDOI2010」外星千足虫 高斯消元

    问题描述 LG2447 BZOJ1923 题解 显然是一个高斯消元,但是求的东西比较奇怪 发现这个方程组只关心奇偶性,于是可以用一个\(\mathrm{bitset}\)进行优化,用xor来进行消元操 ...

  8. Wireshark的简单使用

    TCP包 先看一下Wireshark抓到的TCP的包对应的协议层: Frame:对应是物理层,主要是传输bit流. Ethernet:数据链路层,传输数据帧,二层通信主要是通过mac地址. Inter ...

  9. K12

    K12,教育类专用名词(kindergarten through twelfth grade),是学前教育至高中教育的缩写,现在普遍被用来代指基础教育. K-12教育是美国基础教育的统称.“K12”中 ...

  10. Java递归实现阶乘

    import java.util.Scanner; public class Factorial { public static void main(String[] args) { Scanner ...