Python的PrettyTable模块

1.PrettyTable介绍与基本使用

在使用Python查询表格数据的时候,直接print出来的话。数据杂乱无章,这个使用就可以使用PrettyTable模块来解决这个问题。如下图:

Python的PrettyTable模块_数据库

这样在输出的窗口可以很清晰看到所需要的信息。那么类似这种表格要怎么做出来呢?没错,使用PrettyTable就会把事情变得很简单

2.PrettyTable的安装

pip3 install prettytable

3.PrettyTable的使用

1.添加表头、添加行、添加列

类似于数据库中的表,由表头(或者说字段名),以及每一行的内容组成。

# ======================
# -*-coding: Utf-8 -*-
# ======================
from prettytable import PrettyTable
# 传入的name,age,country相当于表头
td = PrettyTable(["name","age","country"])
# 调用add_row 添加行记录
td.add_row(["Michael","23","China"])
td.add_row(["Bob","24","USA"])
td.add_row(["Jack","25","UK"])
print(td)

Python的PrettyTable模块_数据_02

2.临时添加一列

# ======================
# -*-coding: Utf-8 -*-
# ======================
from prettytable import PrettyTable
# 传入的name,age,country相当于表头
td = PrettyTable(["name","age","country"])
# 调用add_row 添加行记录
td.add_row(["Michael","23","China"])
td.add_row(["Bob","24","USA"])
td.add_row(["Jack","25","UK"])

td.add_column("gender",["male","male","female"])

print(td)

Python的PrettyTable模块_数据_03

输出内容是不是和 MySQL数据库类似呢。

此外 prettytable 还支持从 csv、数据库、html 等数据源中导入数据,但说实话,从数据源读取数据我们一般使用 pandas,并且还会伴随着数据处理。而使用 prettytable 只是为了让程序中产生的信息,能够以结构化的形式打印,很少会从文件或数据库里面读数据

3.输出指定行、指定列

# ======================
# -*-coding: Utf-8 -*-
# ======================
from prettytable import PrettyTable
# 传入的name,age,country相当于表头
td = PrettyTable(["name","age","country"])
# 调用add_row 添加行记录
td.add_row(["Michael","23","China"])
td.add_row(["Bob","24","USA"])
td.add_row(["Jack","25","UK"])

td.add_column("gender",["male","male","female"])

# 只输出name和age列
# start 和 end 参数可以控制输出的起始和结束位置
print(td.get_string(fields=["name","age"],start=1,end=3))

Python的PrettyTable模块_添加行_04

4.设置对齐方式

# ======================
# -*-coding: Utf-8 -*-
# ======================
from prettytable import PrettyTable
# 传入的name,age,country相当于表头
td = PrettyTable(["name","age","country"])
# 调用add_row 添加行记录
td.add_row(["Michael","23","China"])
td.add_row(["Bob","24","USA"])
td.add_row(["Jack","25","UK"])

td.add_column("gender",["male","male","female"])

# l代表左对齐,c表示居中,r表示右对齐
td.align['name'] = "l"
td.align['age'] = 'c'
td.align['country'] = "r"
# 输出表格
print(td)

Python的PrettyTable模块_数据库_05

5.设置边框样式

在 PrettyTable 中,边框由三个部分组成:横边框,竖边框,和边框连接符,我们都可以修改

# ======================
# -*-coding: Utf-8 -*-
# ======================
from prettytable import PrettyTable
# 传入的name,age,country相当于表头
td = PrettyTable(["name","age","country"])
# 调用add_row 添加行记录
td.add_row(["Michael","23","China"])
td.add_row(["Bob","24","USA"])
td.add_row(["Jack","25","UK"])

td.add_column("gender",["male","male","female"])

# 是否显示边框,默认为True
td.border = True

# 横边框
td.horizontal_char = "^"
# 竖边框
td.vertical_char = '>'
# 边框连接符
td.junction_char = '~'

# 输出表格
print(td)

Python的PrettyTable模块_python_06

6.根据csv导入

prettytable支持从csv文件中导入数据并创建表格,需要注意的是,字符串需要加上引号

# ======================
# -*-coding: Utf-8 -*-
# ======================
from prettytable import from_csv
with open('test.csv') as f:
    table = from_csv(f)
print(table)

Python的PrettyTable模块_python_07

7.从数据库中导入

# ======================
# -*-coding: Utf-8 -*-
# ======================
import pymysql
from prettytable import from_db_cursor

connection = pymysql.connect(host='127.0.0.1', user='root', password='123456', port=3306, db='uniform_db')
cursor = connection.cursor()
cursor.execute("select * from uniform_a_v1 limit 10;")
mytable = from_db_cursor(cursor)
print(mytable)

Python的PrettyTable模块_添加行_08

8.表数据的删除

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

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

9.对表格进行排序

# ======================
# -*-coding: Utf-8 -*-
# ======================
from prettytable import PrettyTable
# 传入的name,age,country相当于表头
td = PrettyTable(["name","age","country"])
# 调用add_row 添加行记录
td.add_row(["Michael","23","China"])
td.add_row(["Bob","24","USA"])
td.add_row(["Jack","25","UK"])

td.add_column("gender",["male","male","female"])
# 按照某一列进行排序
print(td.get_string(sortby="age"))
# 按照某一列进行逆序排序
print(td.get_string(sortby="age",reversesort=True))

Python的PrettyTable模块_添加行_09

10.更改颜色属性

# ======================
# -*-coding: Utf-8 -*-
# ======================
from prettytable.colortable import ColorTable,Themes,Theme

x = ColorTable(theme = Themes.OCEAN)
# # ANSI颜色值设置具体参考:https://zh.wikipedia.org/wiki/ANSI%E8%BD%AC%E4%B9%89%E5%BA%8F%E5%88%97
# x = ColorTable(theme=Theme(default_color="33", # 黄色
#                             vertical_color="42", # 绿色
#                             horizontal_color="35", #紫色
#                             junction_color="41",)) # 红色

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(x)

Python的PrettyTable模块_数据_10