Python 官方文档:入门教程 => 点击学习
这篇文章主要介绍“python怎么利用PrettyTable美化表格”,在日常操作中,相信很多人在Python怎么利用PrettyTable美化表格问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”python怎
这篇文章主要介绍“python怎么利用PrettyTable美化表格”,在日常操作中,相信很多人在Python怎么利用PrettyTable美化表格问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”python怎么利用PrettyTable美化表格”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
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 | 男 |# +-----------+-----+--------+--------+-----+
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 |# *++++++++++*++++*+++++++*+++++++*
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怎么利用PrettyTable美化表格”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注编程网网站,小编会继续努力为大家带来更多实用的文章!
--结束END--
本文标题: python怎么利用PrettyTable美化表格
本文链接: https://lsjlt.com/news/326254.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0