prettytable是python的一个控制表格的工具库。支持例如列填充的宽度,文本的对齐方式或表格边框。 我们可以对数据进行排序。
1.环境
ubuntu20.04/windows
# python lib
prettytable==2.4.0
2.prettytable的简单使用
from prettytable import PrettyTable
x = PrettyTable() # 创建
x.field_names = [] # 表头
x.add_row() # 内容
print(x) # 打印输出
3. prettytable显示颜色实验
Python基础之控制台输出颜色_PythonGo-CSDN博客_python 输出颜色
Python中时间与时间戳之间的转换_null的专栏-CSDN博客_python时间转换时间戳
代码如下:
# -*- coding: UTF-8 -*-
'''
@author: gu
@contact: 1065504814@qq.com
@time: 2021/11/16 下午11:08
@file: prettytable_test.py
@desc:
'''
from prettytable import PrettyTable
import time
def vis_table():
x = PrettyTable() # 创建
x.field_names = ["Time", "State"] # 表头
x.add_row([time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(int(time.time()))), "normal"])
time.sleep(1)
x.add_row([time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(int(time.time()))), "abnormal"])
print(x) # 终端打印表格
def vis_color_table():
x = PrettyTable() # 创建
x.field_names = ["Time", "State"] # 表头
x.add_row([time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(int(time.time()))), "normal"])
time.sleep(1)
x.add_row([time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(int(time.time()))), "\033[31mabnormal\033[0m"])
print(x) # 终端打印表格
if __name__ == "__main__":
vis_table()
vis_color_table()
结果如下: