1-Python - prettytable
about
prettytable模块可以用来美化输出。
install
pip install prettytable
pip install -i https://pypi.doubanio.com/simple prettytable==2.0.0
usage
基本用法
import prettytable
from faker import Faker # pip install Faker
fk = Faker(locale='zh_CN')
table = prettytable.PrettyTable(['序号', '姓名', '密码', '地址'])
for i in range(1, 6):
table.add_row([i, fk.name(), fk.password(special_chars=False), fk.address()])
print(table)
"""
+------+--------+------------+--------------------------------------+
| 序号 | 姓名 | 密码 | 地址 |
+------+--------+------------+--------------------------------------+
| 1 | 朱淑华 | QClSmJFt6L | 贵州省济南县江北太原路E座 229737 |
| 2 | 周宁 | ehB7SvDhf6 | 北京市洁县清城荆门街F座 350378 |
| 3 | 廖玉英 | 2WObppm07t | 天津市秀梅市璧山马路G座 873881 |
| 4 | 傅军 | D5TpvZxh2U | 天津市哈尔滨市璧山石家庄路j座 282678 |
| 5 | 刘丹丹 | blGHSKNj5M | 福建省想市普陀济南街U座 959990 |
+------+--------+------------+--------------------------------------+
"""
freestyle
import prettytable
from faker import Faker # pip install Faker
fk = Faker(locale='zh_CN')
table = prettytable.PrettyTable(['序号', '姓名', '密码', '地址'])
# table.set_style(prettytable.DEFAULT)
# table.set_style(prettytable.ORGMODE) # 跟默认差不多
# table.set_style(prettytable.MARKDOWN) # markdown样式
# table.set_style(prettytable.PLAIN_COLUMNS) # 无边框
# table.set_style(prettytable.MSWORD_FRIENDLY) # 无横线
table.set_style(prettytable.RANDOM) # 随机
for i in range(1, 6):
table.add_row([i, fk.name(), fk.password(special_chars=False), fk.address()])
print(table)
除了上述的几种样式,也可以自定义表格样式:
- 设定左对齐:
tb.align = "l"
- 设定数字输出格式:
tb.float_format = "2.2"
- 设定边框连接符为
*
:tb.junction_char = "*"
- 设定左侧不填充空白字符:
tb.left_padding_width = 0
将结果输出
import prettytable
from faker import Faker # pip install Faker
fk = Faker(locale='zh_CN')
table = prettytable.PrettyTable(['序号', '姓名', '密码', '地址'])
for i in range(1, 6):
table.add_row([i, fk.name(), fk.password(special_chars=False), fk.address()])
# 输出为 html
# html = table.get_html_string()
# print(html)
# 输出为json
# json_string = table.get_json_string()
# print(json_string)
# 输出为CSV
# csv = table.get_csv_string()
# print(csv)
# 输出为字符串
# s = table.get_string()
# print(s)
自动根据title列表长度进行表格展示
之前遇到一个需求,就是根据sql语句自动输出结果列数,比如select * from table
,展示所有的匹配字段;当select id,name from table
时,只输出id
和name
这两列:
from prettytable import PrettyTable
data_list = [
{'id': '1', 'name': 'zhangkai', 'age': '22', 'phone': '13651054608', 'dep': 'IT', 'dt': '2013-04-01'},
{'id': '2', 'name': 'likai', 'age': '28', 'phone': '13451024608', 'dep': 'HR', 'dt': '2015-01-07'},
{'id': '3', 'name': 'wangkai', 'age': '21', 'phone': '13451054608', 'dep': 'IT', 'dt': '2017-04-01'},
{'id': '4', 'name': 'zhaokai', 'age': '44', 'phone': '15653354208', 'dep': 'Sales', 'dt': '2016-02-01'},
{'id': '5', 'name': 'sunkuai', 'age': '23', 'phone': '13351024606', 'dep': 'IT', 'dt': '2013-03-16'}
]
# select * from table;
title1 = ['id', 'name', 'age', 'phone', 'dep', 'dt']
# select id,name,age from table;
title2 = ['id', 'name', 'age']
table = PrettyTable(title2)
for i in data_list:
table.add_row([i[j] for j in title2]) # 只展示title列表中的字段
print(table)
"""
+----+----------+-----+
| id | name | age |
+----+----------+-----+
| 1 | zhangkai | 22 |
| 2 | likai | 28 |
| 3 | wangkai | 21 |
| 4 | zhaokai | 44 |
| 5 | sunkuai | 23 |
+----+----------+-----+
"""
that's all, see also: