python实用库:PrettyTable 学习
python实用库:PrettyTable 学习
PrettyTable说明
PrettyTable 是python中的一个第三方库,可用来生成美观的ASCII格式的表格,十分实用。
以下为官方介绍:
A simple Python library for easily displaying tabular data in a visually appealing ASCII table format.
PrettyTable is a simple Python library designed to make it quick and easy to represent tabular data in visually appealing ASCII tables. It was inspired by the ASCII tables used in the PostgreSQL shell psql. PrettyTable allows for selection of which columns are to be printed, independent alignment of columns (left or right justified or centred) and printing of “sub-tables” by specifying a row range.
PrettyTable安装
使用pip即可十分方便的安装PrettyTable,如下:
pip install PrettyTable
PrettyTable使用示例
github上有PrettyTable的使用说明,链接如下:https://github.com/dprince/python-prettytable
以下是具体的使用示例:
import prettytable as pt
## 按行添加数据
tb = pt.PrettyTable()
tb.field_names = ["City name", "Area", "Population", "Annual Rainfall"]
tb.add_row(["Adelaide",1295, 1158259, 600.5])
tb.add_row(["Brisbane",5905, 1857594, 1146.4])
tb.add_row(["Darwin", 112, 120900, 1714.7])
tb.add_row(["Hobart", 1357, 205556,619.5])
print(tb)
+-----------+------+------------+-----------------+
| 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 |
+-----------+------+------------+-----------------+
## 按列添加数据
tb.add_column('index',[1,2,3,4])
print(tb)
+-----------+------+------------+-----------------+-------+
| City name | Area | Population | Annual Rainfall | index |
+-----------+------+------------+-----------------+-------+
| Adelaide | 1295 | 1158259 | 600.5 | 1 |
| Brisbane | 5905 | 1857594 | 1146.4 | 2 |
| Darwin | 112 | 120900 | 1714.7 | 3 |
| Hobart | 1357 | 205556 | 619.5 | 4 |
+-----------+------+------------+-----------------+-------+
## 使用不同的输出风格
tb.set_style(pt.MSWORD_FRIENDLY)
print('--- style:MSWORD_FRIENDLY -----')
print(tb)
tb.set_style(pt.PLAIN_COLUMNS)
print('--- style:PLAIN_COLUMNS -----')
print(tb)
## 随机风格,每次不同
tb.set_style(pt.RANDOM)
print('--- style:MSWORD_FRIENDLY -----')
print(tb)
tb.set_style(pt.DEFAULT)
print('--- style:DEFAULT -----')
print(tb)
--- style:MSWORD_FRIENDLY -----
| 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 |
--- style:PLAIN_COLUMNS -----
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
--- style:MSWORD_FRIENDLY -----
@ Adelaide 1295 1158259 600.5 @
@ Brisbane 5905 1857594 1146.4@
@ Darwin 112 120900 1714.7@
@ Hobart 1357 205556 619.5 @
--- style:DEFAULT -----
+-----------+------+------------+-----------------+
| 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 |
+-----------+------+------------+-----------------+
## 不打印,获取表格字符串
s = tb.get_string()
print(s)
## 可以只获取指定列或行
s = tb.get_string(fields=["City name", "Population"],start=1,end=4)
print(s)
+-----------+------+------------+-----------------+
| 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 |
+-----------+------+------------+-----------------+
+-----------+------------+
| City name | Population |
+-----------+------------+
| Brisbane | 1857594 |
| Darwin | 120900 |
| Hobart | 205556 |
+-----------+------------+
## 自定义表格输出样式
### 设定左对齐
tb.align = 'l'
### 设定数字输出格式
tb.float_format = "2.2"
### 设定边框连接符为'*"
tb.junction_char = "*"
### 设定排序方式
tb.sortby = "City name"
### 设定左侧不填充空白字符
tb.left_padding_width = 0
print(tb)
*----------*-----*-----------*----------------*
|City name |Area |Population |Annual Rainfall |
*----------*-----*-----------*----------------*
|Adelaide |1295 |1158259 |600.50 |
|Brisbane |5905 |1857594 |1146.40 |
|Darwin |112 |120900 |1714.70 |
|Hobart |1357 |205556 |619.50 |
*----------*-----*-----------*----------------*
## 不显示边框
tb.border = 0
print(tb)
## 修改边框分隔符
tb.set_style(pt.DEFAULT)
tb.horizontal_char = '+'
print(tb)
City name Area Population Annual Rainfall
Adelaide 1295 1158259 600.50
Brisbane 5905 1857594 1146.40
Darwin 112 120900 1714.70
Hobart 1357 205556 619.50
+++++++++++++++++++++++++++++++++++++++++++++++++++
| City name | Area | Population | Annual Rainfall |
+++++++++++++++++++++++++++++++++++++++++++++++++++
| Adelaide | 1295 | 1158259 | 600.50 |
| Brisbane | 5905 | 1857594 | 1146.40 |
| Darwin | 112 | 120900 | 1714.70 |
| Hobart | 1357 | 205556 | 619.50 |
+++++++++++++++++++++++++++++++++++++++++++++++++++
## prettytable也支持输出HTML代码
s = tb.get_html_string()
print(s)
<table>
<tr>
<th>City name</th>
<th>Area</th>
<th>Population</th>
<th>Annual Rainfall</th>
</tr>
<tr>
<td>Adelaide</td>
<td>1295</td>
<td>1158259</td>
<td>600.50</td>
</tr>
<tr>
<td>Brisbane</td>
<td>5905</td>
<td>1857594</td>
<td>1146.40</td>
</tr>
<tr>
<td>Darwin</td>
<td>112</td>
<td>120900</td>
<td>1714.70</td>
</tr>
<tr>
<td>Hobart</td>
<td>1357</td>
<td>205556</td>
<td>619.50</td>
</tr>
</table>
## 使用copy方法复制对象
#tb.set_style(pt.DEFAULT)
tb.horizontal_char = '.'
tb2 = tb.copy()
tb.align = 'l'
tb2.align = 'r'
print(tb)
print(tb2)
## 直接赋值,得到的是索引
tb.horizontal_char = '-'
tb.aliign = 'l'
tb3 = tb
tb3.align = 'r'
print(tb)
print(tb3)
+...........+......+............+.................+
| City name | Area | Population | Annual Rainfall |
+...........+......+............+.................+
| Adelaide | 1295 | 1158259 | 600.50 |
| Brisbane | 5905 | 1857594 | 1146.40 |
| Darwin | 112 | 120900 | 1714.70 |
| Hobart | 1357 | 205556 | 619.50 |
+...........+......+............+.................+
+...........+......+............+.................+
| City name | Area | Population | Annual Rainfall |
+...........+......+............+.................+
| Adelaide | 1295 | 1158259 | 600.50 |
| Brisbane | 5905 | 1857594 | 1146.40 |
| Darwin | 112 | 120900 | 1714.70 |
| Hobart | 1357 | 205556 | 619.50 |
+...........+......+............+.................+
+-----------+------+------------+-----------------+
| City name | Area | Population | Annual Rainfall |
+-----------+------+------------+-----------------+
| Adelaide | 1295 | 1158259 | 600.50 |
| Brisbane | 5905 | 1857594 | 1146.40 |
| Darwin | 112 | 120900 | 1714.70 |
| Hobart | 1357 | 205556 | 619.50 |
+-----------+------+------------+-----------------+
+-----------+------+------------+-----------------+
| City name | Area | Population | Annual Rainfall |
+-----------+------+------------+-----------------+
| Adelaide | 1295 | 1158259 | 600.50 |
| Brisbane | 5905 | 1857594 | 1146.40 |
| Darwin | 112 | 120900 | 1714.70 |
| Hobart | 1357 | 205556 | 619.50 |
+-----------+------+------------+-----------------+
python实用库:PrettyTable 学习的更多相关文章
- Python Pandas库的学习(一)
今天我们来学习一下Pandas库,前面我们讲了Numpy库的学习 接下来我们学习一下比较重要的库Pandas库,这个库比Numpy库还重要 Pandas库是在Numpy库上进行了封装,相当于高级Num ...
- python实用库
参考:https://github.com/programthink/opensource/blob/master/libs/python.wiki#35_ Python 开源库及示例代码 Table ...
- 这十个Python常用库,学习Python的你必须要知道!
想知道Python取得如此巨大成功的原因吗?只要看看Python提供的大量库就知道了 包括原生库和第三方库.不过,有这么多Python库,有些库得不到应有的关注也就不足为奇了.此外,只在一个领域里的工 ...
- 这十个Python常用库?学习Python的你必须要知道!
想知道Python取得如此巨大成功的原因吗?只要看看Python提供的大量库就知道了 ,包括原生库和第三方库.不过,有这么多Python库,有些库得不到应有的关注也就不足为奇了.此外,只在一个领域里的 ...
- python 标准库基础学习之开发工具部分1学习
#2个标准库模块放一起学习,这样减少占用地方和空间#标准库之compileall字节编译源文件import compileall,re,sys#作用是查找到python文件,并把它们编译成字节码表示, ...
- Python Pandas库的学习(二)
今天我们继续讲下Python中一款数据分析很好的库.Pandas的学习 接着上回讲到的,如果有人听不懂,麻烦去翻阅一下我前面讲到的Pandas学习(一) 如果我们在数据中,想去3,4,5这几行数据,那 ...
- Python asyncio库的学习和使用
因为要找工作,把之前自己搞的爬虫整理一下,没有项目经验真蛋疼,只能做这种水的不行的东西...T T,希望找工作能有好结果. 之前爬虫使用的是requests+多线程/多进程,后来随着前几天的深入了解 ...
- Python Pandas库的学习(三)
今天我们来继续讲解Python中的Pandas库的基本用法 那么我们如何使用pandas对数据进行排序操作呢? food.sort_values("Sodium_(mg)",inp ...
- 10个顶级Python实用库,推荐你试试!
为什么我喜欢Python?对于初学者来说,这是一种简单易学的编程语言,另一个原因:大量开箱即用的第三方库,正是23万个由用户提供的软件包使得Python真正强大和流行. 在本文中,我挑选了15个最有用 ...
随机推荐
- 2018.11.05 NOIP模拟 规避(最短路计数)
传送门 正难则反. 考虑计算两人相遇的方案数. 先正反跑一遍最短路计数. 然后对于一条在最短路上的边(u,v)(u,v)(u,v),如果(dis(s,u)*2<total&&di ...
- Eclipse创建Dynamic Web部署
Eclipse创建Dynamic Web部署 http://blog.csdn.net/sweblish/article/details/6686046 Eclipse3.x中热部署项目,启动错误问题 ...
- 负载均衡下 tomcat session 共享
概述 在分布式部署的情况下,每台tomcat 都会有自己的session ,这样如果 用户A 在tomcat1 下登录,在tomcat2 下并没有session信息.如果 tomcat1宕机,tomc ...
- BZOJ 1024 [SCOI2009]生日快乐 (搜索)
1024: [SCOI2009]生日快乐 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 3025 Solved: 2201[Submit][Statu ...
- 【慕课网实战】Spark Streaming实时流处理项目实战笔记十之铭文升级版
铭文一级: 第八章:Spark Streaming进阶与案例实战 updateStateByKey算子需求:统计到目前为止累积出现的单词的个数(需要保持住以前的状态) java.lang.Illega ...
- 7-12 How Long Does It Take
Given the relations of all the activities of a project, you are supposed to find the earliest comple ...
- 【python-字典】判断python字典中key是否存在的
一般有两种通用做法: 第一种方法:使用自带函数实现: 在python的字典的属性方法里面有一个has_key()方法: #生成一个字典 d = {'name':Tom, 'age':10, 'Tel' ...
- POJ2536 Gopher II(二分图最大匹配)
Gopher II Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9005 Accepted: 3724 Descrip ...
- hdu 1576 A/B 【扩展欧几里德】
题目 A/9973=n 那么:n= A - A / 9973 * 9973 --① 设:A/B=x 则A=B*x,代入① 得 n=B*x-A/9973*9973 然后这个方程中的A/9973 ...
- excel设定备选值
excel设定备选值 有的时候我们要人为向excel中某一列添加数据,可以通过下面的方法,为这列设定备选值. 操作方法 选中excel表格的一列,选择 数据 -- 有效性 -- 允许: 选择 序列 ...