更多课程 选择中心


Python培训

400-111-8989

Python还能做出漂亮的表格?一起来瞧瞧


最近在用python写一个小工具,这个工具主要就是用来管理各种资源的信息,比如阿里云的ECS等信息,因为我工作的电脑使用的是LINUX,所以就想着用 Python写一个命令行的管理工具,基本的功能就是同步阿里云的资源的信息到数据库,然后可以使用命令行查询。

因为信息是展现在命令行中的,众所周知,命令行展现复杂的文本看起来着实累人,于是就想着能像表格那样展示,那看起来就舒服多了。

prettytable库就是这么一个工具,prettytable可以打印出美观的表格,并且对中文支持相当好(如果有试图自己实现打印表格,你就应该知道处理中文是多么的麻烦)

说明:本文使用Markdown语法编写,为了展示方便,以及复制方便,所以本文中没有使用截图,因为格式控制的问题,文章中的运行结果会出现一些分割线的偏移,在终端中呈现并此问题,请各位手动去操作验证。

安装

prettytable并非python的内置库,通过 pip install prettytable即可安装。

演示一个小示例:我们先来看一个示例:

#!/usr/bin/python

#**coding:utf-8**

import sys

from prettytable import PrettyTable

reload(sys)

sys.setdefaultencoding('utf8')

table = PrettyTable(['编号','云编号','名称','IP地址'])

table.add_row(['1','server01','服务器01','172.16.0.1'])

table.add_row(['2','server02','服务器02','172.16.0.2'])

table.add_row(['3','server03','服务器03','172.16.0.3'])

table.add_row(['4','server04','服务器04','172.16.0.4'])

table.add_row(['5','server05','服务器05','172.16.0.5'])

table.add_row(['6','server06','服务器06','172.16.0.6'])

table.add_row(['7','server07','服务器07','172.16.0.7'])

table.add_row(['8','server08','服务器08','172.16.0.8'])

table.add_row(['9','server09','服务器09','172.16.0.9'])

print(table)

以上示例运行结果如下:

linuxops@deepin:~$ python p.py

+------+----------+----------+------------+

| 编号 | 云编号 | 名称 | IP地址 |

+------+----------+----------+------------+

| 1 | server01 | 服务器01 | 172.16.0.1 |

| 2 | server02 | 服务器02 | 172.16.0.2 |

| 3 | server03 | 服务器03 | 172.16.0.3 |

| 4 | server04 | 服务器04 | 172.16.0.4 |

| 5 | server05 | 服务器05 | 172.16.0.5 |

| 6 | server06 | 服务器06 | 172.16.0.6 |

| 7 | server07 | 服务器07 | 172.16.0.7 |

| 8 | server08 | 服务器08 | 172.16.0.8 |

| 9 | server09 | 服务器09 | 172.16.0.9 |

+------+----------+----------+------------+

在以上的示例中,我们通过form导入了表格库。table实例化了一个表格库,并且添加了['编号','云编号','名称','IP地址']为表头,如果没有添加表头,那么会以默认的Field+编号显示,例如:

+---------+----------+----------+------------+

| Field 1 | Field 2 | Field 3 | Field 4 |

+---------+----------+----------+------------+

所以为更直观看出每一列的意义,还是要添加表头的。

添加数据

prettytable提供了多种的添加数据的方式,最常用的应该就是按行按列添加数据了。

A、按行添加数据 table.add_row

在上面简单的示例中,我们就是按行添加数据的。

添加的数据必须要是列表的形式,而且数据的列表长度要和表头的长度一样。在实际的使用中,我们应该要关注到添加的数据是否和表头对应,这一点很重要。

B、按列添加数据 table.add_column()

看下面的示例:

#!/usr/bin/python

#**coding:utf-8**

import sys

from prettytable import PrettyTable

reload(sys)

sys.setdefaultencoding('utf8')

table = PrettyTable()

table.add_column('项目', ['编号','云编号','名称','IP地址'])

table.add_column('值', ['1','server01','服务器01','172.16.0.1'])

print(table)

运行结果如下:

+-------+--------+------------+

| index | 项目 | 值 |

+-------+--------+------------+

| 1 | 编号 | 1 |

| 2 | 云编号 | server01 |

| 3 | 名称 | 服务器01 |

| 4 | IP地址 | 172.16.0.1 |

+-------+--------+------------+

以上示例中,我们通过add_column来按列添加数据,按列添加数据不需要在实例化表格的时候制定表头,它的表头是在添加列的时候指定的。

table.add_column('项目', ['编号','云编号','名称','IP地址']) 这一行代码为例,项目指定了这个列的表头名为"项目",['编号','云编号','名称','IP地址']为列的值,同样为列表。

C、从csv文件添加数据

PrettyTable不仅提供了手动按行按列添加数据,也支持直接从csv文件中读取数据。

#!/usr/bin/python

#**coding:utf-8**

import sys

from prettytable import PrettyTable

from prettytable import from_csv

reload(sys)

sys.setdefaultencoding('utf8')

table = PrettyTable()

fp = open("res.csv", "r")

table = from_csv(fp)

print(table)

fp.close()

如果要读取cvs文件数据,必须要先导入from_csv,否则无法运行。上面的示例运行结果如下:

+------+----------+----------+------------+

| 编号 | 云编号 | 名称 | IP地址 |

+------+----------+----------+------------+

| 1 | server01 | 服务器01 | 172.16.0.1 |

| 2 | server02 | 服务器02 | 172.16.0.2 |

| 3 | server03 | 服务器03 | 172.16.0.3 |

| 4 | server04 | 服务器04 | 172.16.0.4 |

| 5 | server05 | 服务器05 | 172.16.0.5 |

| 6 | server06 | 服务器06 | 172.16.0.6 |

| 7 | server07 | 服务器07 | 172.16.0.7 |

| 8 | server08 | 服务器08 | 172.16.0.8 |

| 9 | server09 | 服务器09 | 172.16.0.9 |

+------+----------+----------+------------+

csv文件不能通过xls直接重命名得到,会报错。如果是xls文件,请用另存为csv获得csv文件

D、从sql查询值添加

从数据库查询出来的数据可以直接导入到表格打印,下面的例子使用了sqlite3,如果使用的是mysql也是一样的,只要能查询到数据就能导入到表格中

#!/usr/bin/python

#**coding:utf-8**

import sys

from prettytable import PrettyTable

from prettytable import from_db_cursor

import sqlite3

reload(sys)

sys.setdefaultencoding('utf8')

conn = sqlite3.connect("/tmp/aliyun.db")

cur = conn.cursor()

cur.execute("SELECT * FROM res")

table = from_db_cursor(cur)

print(table)

运行结果如下:

+------+----------+----------+------------+

| 编号 | 云编号 | 名称 | IP地址 |

+------+----------+----------+------------+

| 1 | server01 | 服务器01 | 172.16.0.1 |

| 2 | server02 | 服务器02 | 172.16.0.2 |

| 3 | server03 | 服务器03 | 172.16.0.3 |

| 4 | server04 | 服务器04 | 172.16.0.4 |

| 5 | server05 | 服务器05 | 172.16.0.5 |

| 6 | server06 | 服务器06 | 172.16.0.6 |

| 7 | server07 | 服务器07 | 172.16.0.7 |

| 8 | server08 | 服务器08 | 172.16.0.8 |

| 9 | server09 | 服务器09 | 172.16.0.9 |

+------+----------+----------+------------+

E、从HTML导入数据

支持从html的表格中导入,请看下面这个例子:

#!/usr/bin/python

#**coding:utf-8**

import sys

from prettytable import PrettyTable

from prettytable import from_html

reload(sys)

sys.setdefaultencoding('utf8')

html_string='''<table>

<tr>

<th>编号</th>

<th>云编号</th>

<th>名称</th>

<th>IP地址</th>

</tr>

<tr>

<td>1</td>

<td>server01</td>

<td>服务器01</td>

<td>172.16.0.1</td>

</tr>

<tr>

<td>2</td>

<td>server02</td>

<td>服务器02</td>

<td>172.16.0.2</td>

</tr>

</table>'''

table = from_html(html_string)

print(table[0])

运行结果如下:

+------+----------+----------+------------+

| 编号 | 云编号 | 名称 | IP地址 |

+------+----------+----------+------------+

| 1 | server01 | 服务器01 | 172.16.0.1 |

| 2 | server02 | 服务器02 | 172.16.0.2 |

+------+----------+----------+------------+

如上示例中,我们可以导入html的表格,但是不一样的地方是print语句,使用html表格导入数据的时候print的必须是列表中的第一个元素,否则有可能会报[]这样的错误。

这是因为table并不是PrettyTable对象,而是包含单个PrettyTable对象的列表,它通过解析html而来,所以无法直接打印table,而需要打印table[0]。

达内每年输送10万+人才,18年来帮助80万学员高薪就业;协助16万家企业解决人才需求。拥有完善的就业保障体系,116万家招聘雇主合作企业。每天产生数千个招聘岗位,提供更多就业机会给到达内学员。找Python培训,选达内就对了!

版权声明:转载文章来自公开网络,版权归作者本人所有,推送文章除非无法确认,我们都会注明作者和来源。如果出处有误或侵犯到原作者权益,请与我们联系删除或授权事宜。

预约申请免费试听课

填写下面表单即可预约申请免费试听! 怕学不会?助教全程陪读,随时解惑!担心就业?一地学习,可全国推荐就业!

上一篇:Python正则表达式字符串替换/分割的方法?
下一篇:Python怎么做出漂亮的表格?

如何自学Python?

说一说python中的几个基础语法

为什么Python类语法应该不同?

0基础入门Python,3 个常识点必须先了解!

  • 扫码领取资料

    回复关键字:视频资料

    免费领取 达内课程视频学习资料

Copyright © 2023 Tedu.cn All Rights Reserved 京ICP备08000853号-56 京公网安备 11010802029508号 达内时代科技集团有限公司 版权所有

选择城市和中心
黑龙江省

吉林省

河北省

湖南省

贵州省

云南省

广西省

海南省