欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > python >内容正文

python

Python——PrettyTable

发布时间:2025/3/15 python 42 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Python——PrettyTable 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

目录

一、说明

二、安装

三、导入

四、基本使用 

        1、创建表格

        2、设置表的名称

        3、按行/列添加数据: 

        4、一次性添加多行

        5、根据CSV导入

         6、从数据库中导入

        7、表数据的删除

        8、显示表格

        9、显示指定的列 

​        10、显示指定的行


一、说明

        PrettyTable 是python中的一个第三方库,可用来生成美观的ASCII格式的表格,十分实用

二、安装

        使用pip即可十分方便的安装PrettyTable(命令行窗口安装),如下:

        先windows+R打开命令行窗口

        然后输入pip install PrettyTable

pip install PrettyTable

三、导入

        from prettytable import PrettyTable 或者 import prettytable as pt

四、基本使用 

        1、创建表格

创建表:tb = pt.PrettyTable()或x = PrettyTable()按行/列添加数据:tb.add_row( <llist> )tb.add_column( <llist> )

        2、设置表的名称

x.title = 'Table 1 City Info'

        3、按行/列添加数据: 

>>> ## 按行添加数据 ... 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 | +-----------+------+------------+-----------------+-------+ >>>

        4、一次性添加多行

x.field_names = ["City name", "Area", "Population", "Annual Rainfall"] x.add_rows([["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的第一个参数表示列的字段名,为字符串,第二个参数为列表,即添加到该列的数据。没有一次性添加多列的方法。

        5、根据CSV导入

                PrettyTable不仅提供了手动按行按列添加数据,也支持直接从csv文件中读取数据,需要注意的是,字符串需要加上引号。如果要读取cvs文件数据,必须要先导入from_csv,否则无法运行

from prettytable import from_csv with open("CityInfo.csv") as fp:mytable = from_csv(fp) print(mytable)

         6、从数据库中导入

                从数据库查询出来的数据可以直接导入到表格打印

#!/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 |
+------+----------+----------+------------+ 

        7、表数据的删除

   prettytable提供四种方法用于删除数据:

del_row:删除某行,允许传入一个整数参数,(从0开始)。
del_column:删除某列,允许传入一个字符串,表示要删除的列的字段名。
clear_rows:删除所有数据,但保存列的字段名。
clear:删除所有数据,包括列的字段名。

        8、显示表格

print(x) mystring = x.get_string()

get_string()函数可以将上面print的结果直接转化为字符串,可以将这个结果写到文本文件里。当然也可以打印出来。

print(x.get_string())

        9、显示指定的列 

print(x.get_string(fields=["City name", "Population"]))


        10、显示指定的行

print(x.get_string(start=1, end=4))


 

总结

以上是生活随笔为你收集整理的Python——PrettyTable的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。