欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

python 利用 PrettyTable 美化表格

时间:2022-06-09 09:20:12|栏目:Python代码|点击:

一、安装

pip install PrettyTable

二、按行设置数据

import prettytable as pt

# 按行添加数据
tb = pt.PrettyTable()
tb.field_names = ['name', 'age', 'height', 'weight']
tb.add_row(['autofelix', 25, 174, 65])
tb.add_row(['大神', 23, 164, 55])
tb.add_row(['飞兔小哥', 27, 184, 69.5])

print(tb)

# +-----------+-----+--------+--------+
# | name | age | height | weight |
# +-----------+-----+--------+--------+
# | autofelix | 25 | 174 | 65 |
# | 大神 | 23 | 164 | 55 |
# | 飞兔小哥 | 27 | 184 | 69.5 |
# +-----------+-----+--------+--------+

三、按列添加

import prettytable as pt

# 按行添加数据
tb = pt.PrettyTable()
tb.field_names = ['name', 'age', 'height', 'weight']
tb.add_row(['autofelix', 25, 174, 65])
tb.add_row(['大神', 23, 164, 55])
tb.add_row(['飞兔小哥', 27, 184, 69.5])
# 按列添加数据
tb.add_column('sex',['男', '女', '男'])

print(tb)

# +-----------+-----+--------+--------+-----+
# | name | age | height | weight | sex |
# +-----------+-----+--------+--------+-----+
# | autofelix | 25 | 174 | 65 | 男 |
# | 大神 | 23 | 164 | 55 | 女 |
# | 飞兔小哥 | 27 | 184 | 69.5 | 男 |
# +-----------+-----+--------+--------+-----+

四、输出风格

  • MSWORD_FRIENDLY:MSWORD_FRIENDLY输出风格
  • PLAIN_COLUMNS:PLAIN_COLUMNS输出风格
  • RANDOM:每次随机输出风格
  • DEFAULT:默认输出风格
import prettytable as pt

# 按行添加数据
tb = pt.PrettyTable()
tb.field_names = ['name', 'age', 'height', 'weight']
tb.add_row(['autofelix', 25, 174, 65])
tb.add_row(['大神', 23, 164, 55])
tb.add_row(['飞兔小哥', 27, 184, 69.5])
# 风格
tb.set_style(pt.MSWORD_FRIENDLY)

print(tb)

# | name | age | height | weight |
# | autofelix | 25 | 174 | 65 |
# | 大神 | 23 | 164 | 55 |
# | 飞兔小哥 | 27 | 184 | 69.5 |

五、获取字符串

import prettytable as pt

# 按行添加数据
tb = pt.PrettyTable()
tb.field_names = ['name', 'age', 'height', 'weight']
tb.add_row(['autofelix', 25, 174, 65])
tb.add_row(['大神', 23, 164, 55])
tb.add_row(['飞兔小哥', 27, 184, 69.5])

# 不打印,获取表格字符串
s1 = tb.get_string()
print(s1)

# +-----------+-----+--------+--------+
# | name | age | height | weight |
# +-----------+-----+--------+--------+
# | autofelix | 25 | 174 | 65 |
# | 大神 | 23 | 164 | 55 |
# | 飞兔小哥 | 27 | 184 | 69.5 |
# +-----------+-----+--------+--------+

# 或者可以只获取指定列或行
s2 = tb.get_string(fields=['name', 'age'], start=1, end=4)
print(s2)

# +----------+-----+
# | name | age |
# +----------+-----+
# | 大神 | 23 |
# | 飞兔小哥 | 27 |
# +----------+-----+

六、表格样式设置

import prettytable as pt

# 按行添加数据
tb = pt.PrettyTable()
tb.field_names = ['name', 'age', 'height', 'weight']
tb.add_row(['autofelix', 25, 174, 65])
tb.add_row(['大神', 23, 164, 55])
tb.add_row(['飞兔小哥', 27, 184, 69.5])

# 设定左对齐
tb.align = 'l'
# 设定数字输出格式
tb.float_format = '2.2'
# 设定边框连接符为'*"
tb.junction_char = '*'
# 设定排序方式
tb.sortby = 'age'
# 设定左侧不填充空白字符
tb.left_padding_width = 0
# 不显示边框
# tb.border = 0
# 修改边框分隔符
tb.horizontal_char = '+'

print(tb)

# *++++++++++*++++*+++++++*+++++++*
# |name |age |height |weight |
# *++++++++++*++++*+++++++*+++++++*
# |大神 |23 |164 |55 |
# |autofelix |25 |174 |65 |
# |飞兔小哥 |27 |184 |69.50 |
# *++++++++++*++++*+++++++*+++++++*

七、输出成HTML

import prettytable as pt

# 按行添加数据
tb = pt.PrettyTable()
tb.field_names = ['name', 'age', 'height', 'weight']
tb.add_row(['autofelix', 25, 174, 65])
tb.add_row(['大神', 23, 164, 55])
tb.add_row(['飞兔小哥', 27, 184, 69.5])

# 输出HTML代码
s = tb.get_html_string()
print(s)

# <table>
# <thead>
# <tr>
# <th>name</th>
# <th>age</th>
# <th>height</th>
# <th>weight</th>
# </tr>
# </thead>
# <tbody>
# <tr>
# <td>autofelix</td>
# <td>25</td>
# <td>174</td>
# <td>65</td>
# </tr>
# <tr>
# <td>大神</td>
# <td>23</td>
# <td>164</td>
# <td>55</td>
# </tr>
# <tr>
# <td>飞兔小哥</td>
# <td>27</td>
# <td>184</td>
# <td>69.5</td>
# </tr>
# </tbody>
# </table>

八、复制

import prettytable as pt

# 按行添加数据
tb = pt.PrettyTable()
tb.field_names = ['name', 'age', 'height', 'weight']
tb.add_row(['autofelix', 25, 174, 65])
tb.add_row(['大神', 23, 164, 55])
tb.add_row(['飞兔小哥', 27, 184, 69.5])

tb.horizontal_char = '.'
tb2 = tb.copy()
tb.align = 'l'
tb2.align = 'r'
print(tb)
print(tb2)

# +...........+.....+........+........+
# | name | age | height | weight |
# +...........+.....+........+........+
# | autofelix | 25 | 174 | 65 |
# | 大神 | 23 | 164 | 55 |
# | 飞兔小哥 | 27 | 184 | 69.5 |
# +...........+.....+........+........+

# +...........+.....+........+........+
# | name | age | height | weight |
# +...........+.....+........+........+
# | autofelix | 25 | 174 | 65 |
# | 大神 | 23 | 164 | 55 |
# | 飞兔小哥 | 27 | 184 | 69.5 |
# +...........+.....+........+........+

上一篇:Python if else条件语句形式详解

栏    目:Python代码

下一篇:Python3.8对可迭代解包的改进及用法详解

本文标题:python 利用 PrettyTable 美化表格

本文地址:http://www.codeinn.net/misctech/204208.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有