Python PrettyTable 模块(美化库)
- Python PrettyTable 模块
- 简介
- 安装
- 示例
- 使用
- 创建表
- 直接创建
- 从已有文件创建
- CSV
- HTML
- SQL
- 添加元素
- 按行添加
- 按列添加
- 输出格式
- ASCII码表
- 直接输出
- 无表格框输出
- HTML表
- 选择子表
- 表排序
- ASCII码表
- 控制表样式
- 自带样式
- 手动控制样式
- 可调整选项
- 用法
- 调整对齐方式的几种方法
- 创建表
- 参考资料
简介
Python通过PrettyTable模块可以将输出内容如表格方式整齐地输出。
安装
pip install prettytable
- 1
示例
from prettytable import PrettyTable
table = PrettyTable(["animal", "ferocity"])
table.add_row(["wolverine", 100])
table.add_row(["grizzly", 87])
table.add_row(["Rabbit of Caerbannog", 110])
table.add_row(["cat", -1])
table.add_row(["platypus", 23])
table.add_row(["dolphin", 63])
table.add_row(["albatross", 44])
table.sort_key("ferocity")
table.reversesort = True
print(table)'''效果图
+----------------------+----------+
| animal | ferocity |
+----------------------+----------+
| Rabbit of Caerbannog | 110 |
| wolverine | 100 |
| grizzly | 87 |
| dolphin | 63 |
| albatross | 44 |
| platypus | 23 |
| cat | -1 |
+----------------------+----------+
'''
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
使用
创建表
直接创建
pt = PrettyTable()
- 1
从已有文件创建
CSV
from prettytable import from_csv
fp = open("mytable.csv", "r")
pt = from_csv(fp)
fp.close()
- 1
- 2
- 3
- 4
HTML
from prettytable import from_html
pts = from_html(html_string)
- 1
- 2
SQL
from prettytable import from_db_cursor
db_cur.execute("SELECT * FROM mytable")
pt = from_db_cursor(db_cur)
- 1
- 2
- 3
添加元素
按行添加
pt.add_row()
- 1
按列添加
pt.add_column()
- 1
输出格式
ASCII码表
直接输出
print(pt)
- 1
无表格框输出
print(pt.get_string())
- 1
HTML表
print(pt.get_html_string())
- 1
选择子表
print(pt.get_string(fields = ["City name", "Population"]))
- 1
#输出前4列
print(pt.get_string(start = 0, end = 3))
- 1
- 2
new_table = old_table[0:3]
print(new_table)
- 1
- 2
表排序
print x.get_string(sortby="Annual Rainfall", reversesort=True)
- 1
控制表样式
自带样式
#参数还可以选择“DEFAULT”、“PLAIN_COLUMNS”
from prettytable import MSWORD_FRIENDLY
x.set_style(MSWORD_FRIENDLY)
print(x)
- 1
- 2
- 3
- 4
手动控制样式
可调整选项
摘自prettytable文档
- border - 布尔类型参数(必须是True或False)。控制表格边框是否显示。
- header - 布尔类型参数(必须是True或False)。控制表格第一行是否作为表头显示。
- header-style - 控制表头信息的大小写。允许的参数值:“cap”(每个单词首字母大写),“title”(除了介词助词首字母大写),“lower”(全部小写)或者None(不改变原内容格式)。默认参数为None。
- hrules - 设置表格内部水平边线。允许的参数值:FRAME,ALL,NONE。注意这些是在prettytable模块内部定义的变量,在使用之前导入或用类似prettytable.FRAME的方法调用。
- vrules - 设置表格内部竖直边线。允许的参数值:FRAME,ALL,NONE。
- align - 水平对齐方式(None,“l”(左对齐),“c”(居中),“r”右对齐)
- valign - 垂直对齐方式(None,“t”(顶部对齐),“m”(居中),“b”底部对齐)
- int_format - 控制整型数据的格式。
- float_format - 控制浮点型数据的格式。
- padding_width - 列数据左右的空格数量。(当左右padding未设置时生效)
- left_padding_width - 列数据左侧的空格数量。
- right_padding_width - 列数据右侧的空格数量。
- vertical_char - 绘制竖直边线的字符,默认为“|”
- horizontal_char - 绘制水平边线的字符,默认为“-”
- junction_char - 绘制水平竖直交汇点的字符,默认为“+”
- border - A boolean option (must be True or False). Controls whether or not a border is drawn around the table.
- header - A boolean option (must be True or False). Controls whether or not the first row of the table is a header showing the names of all the fields.
- header_style - Controls capitalisation of field names in the header. Allowed values: “cap” (capitalise first letter of each word), “title” (title case), “upper” (all upper-case), “lower” (all lower-case) or None (don’t change from original field name setting). Default is None.
- hrules - Controls printing of horizontal rules after rows. Allowed values: FRAME, ALL, NONE - note that these are variables defined inside the prettytable module so make sure you import them or use prettytable.FRAME etc.
- vrules - Controls printing of vertical rules between columns. Allowed values: FRAME, ALL, NONE
- align - Horizontal alignment (None, “l” (left), “c” (centre), “r” (right))
- valign - Vertical alignment (None, “t” (top), “m” (middle) or “b” (bottom))
- int_format - Controls formatting of integer data. This should be a string which can be placed between “%” and “d” in something like print “%d” % 42.
- float_format - Controls formatting of floating point data. This should be a string which can be placed between “%” and “f” in something like print “%f” % 4.2.
- padding_width - Number of spaces on either side of column data (only used if left and right paddings are None).
- left_padding_width - Number of spaces on left hand side of column data.
- right_padding_width - Number of spaces on right hand side of column data.
- vertical_char - Single character string used to draw vertical lines. Default is |.
- horizontal_char - Single character string used to draw horizontal lines. Default is -.
- junction_char - Single character string used to draw line junctions. Default is +.
用法
x = PrettyTable()
x.border = False
x.header = False
x.padding_width = 5
- 1
- 2
- 3
- 4
x = PrettyTable(border=False, header=False, padding_width=5)
- 1
以上两种设置方式等效
调整对齐方式的几种方法
print(x.get_string(align="l"))
- 1
x.align["City name"] = "l"
x.align["Population"] = "c"
x.align["Area"] = "r"
- 1
- 2
- 3
x.align = "l'
- 1
参考资料
- prettytable 文档
使用方法从文档摘取过来的 更多用法请直接参考文档 - python之PrettyTable模块
- Python prettytable.PrettyTable Examples
更多实例提供参考
Python PrettyTable 模块(美化库)相关推荐
- 实战篇一 python常用模块和库介绍
# -_-@ coding: utf-8 -_-@ -- Python 常用模块和库介绍 第一部分:json模块介绍 import json 将一个Python数据结构转换为JSON: dict_ = ...
- 太好了,一分钟带你分清Python的模块、库、包有什么联系和区别?
Python的流行主要依赖于其有众多功能强大的库(Library),Python自带的标准库(Standard Library)可以满足大多数的基础需求,除了函数库以外,模块(Module)和包(Pa ...
- python樱花代码_使用python图形模块turtle库绘制樱花、玫瑰、圣诞树代码实例
今天为大家介绍几个Python"装逼"实例代码,python绘制樱花.玫瑰.圣诞树代码实例,主要使用了turtle库 Python绘制樱花代码实例 动态生成樱花 效果图(这个是动态 ...
- 用python画圣诞树-使用python图形模块turtle库绘制樱花、玫瑰、圣诞树代码实例
今天为大家介绍几个Python"装逼"实例代码,python绘制樱花.玫瑰.圣诞树代码实例,主要使用了turtle库 Python绘制樱花代码实例 动态生成樱花 效果图(这个是动态 ...
- Python PrettyTable 模块
Python PrettyTable 模块 Python PrettyTable 模块 简介 安装 示例 使用 创建表 直接创建 从已有文件创建 CSV HTML SQL 添加元素 按行添加 按列添加 ...
- python使用turtle库绘制一个100长度的十字架_使用python图形模块turtle库绘制樱花、玫瑰、圣诞树代码实例...
使用python图形模块turtle库绘制樱花.玫瑰.圣诞树代码实例 发布时间:2020-08-22 15:57:19 来源:脚本之家 阅读:76 作者:Soul fragments 今天为大家介绍几 ...
- 绘制图形可以使用什么python数据库_使用python图形模块turtle库绘制樱花、玫瑰、圣诞树代码实例...
今天为大家介绍几个Python"装逼"实例代码,python绘制樱花.玫瑰.圣诞树代码实例,主要使用了turtle库 Python绘制樱花代码实例 动态生成樱花 效果图(这个是动态 ...
- python中prettytable模块_Python库: PrettyTable 模块
一 PrettyTable简介 PrettyTable是python中的一个第三方库,可用来生成美观的ASCII格式的表格: 二 PrettyTable安装 使用PIP即可十分方便的安装PrettyT ...
- Python系统模块增强库(xToolkit库)
1 什么是xToolkit库 库xToolkit的中文名字叫X工具集.是python内置库的一个扩展库.把python的datetime,string,list,dist,xthread等数据结构进行 ...
最新文章
- python入门指南bl-Python Flask开源博客系统Bl
- C语言灵魂篇|指针作为函数返回值
- 专访合一智芯杨桦:做AI芯片核心技术的底层设计师
- Git : 每一行命令都算数
- 罗技G29方向盘linux下的开发
- matlab安装与下载
- 神奇的css之 -webkit-background-clip: text; 五彩斑斓的文字效果
- 各种电脑/软件/生活/音乐/动漫/电影技巧汇总,你肯定能发现你需要的使用技巧,你的生活绝对会因此简化很多,具体操作见本专栏相关文章或点击链接
- 基于FPGA的火焰识别系统开发——简化版
- 网络安全初创企业Illumio融资1.25亿美元 摩根领投
- MySQL架构 数据库(1)
- Andorid-foreground 解析
- Air202入坑指南4---UART2(简单使用)
- 最新修复版“WBO中庚杯拳王争霸赛搞笑报名表源码”
- linux中文谐音,GNU和LINUX到底怎么个发音?
- 稳恒(有人透传云)NBIOT模块使用指南(2)
- 华为5G手机发布!5G和AI给世界带来什么?
- XML的基本概念和Android下的使用
- DSP28335通过按键启动LED闪烁功能
- 论文阅读 | Asymmetric Bilateral Motion Estimation for Video Frame Interpolation
热门文章
- matlab如何打开dcm_MatLab 与 visual studio 混合编程环境配置
- 快递鸟电⼦⾯单批量打印流程与注意事项
- python数学公式代码_PythonStudy_‘数学公式计算器’代码
- 【微信小程序/云开发bug解决方案合集】持续更新中(最新22-11-21)
- 竟有比双十一更令人发指的福利……
- 计算机word保存如何操作,如何保存WORD文档内容???急
- 画一个带统计检验的PCoA分析结果
- xp怎么删除计算机管理员用户名和密码,Windows XP 的 Administrator 超级管理员密码忘记了,如何清除?...
- 数字证书有什么作用?
- Oracle12c设置PDB企业管理器登录端口