python 利用 PrettyTable 美化表格
目录
一、安装二、按行设置数据三、按列添加四、输出风格五、获取字符串六、表格样式设置七、输出成HTML八、复制一、安装
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) #
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.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 美化表格的文章就介绍到这了,更多相关PrettyTable 美化表格内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
上一篇:Java 链表实战真题训练
X 关闭
X 关闭
- 15G资费不大降!三大运营商谁提供的5G网速最快?中国信通院给出答案
- 2联想拯救者Y70发布最新预告:售价2970元起 迄今最便宜的骁龙8+旗舰
- 3亚马逊开始大规模推广掌纹支付技术 顾客可使用“挥手付”结账
- 4现代和起亚上半年出口20万辆新能源汽车同比增长30.6%
- 5如何让居民5分钟使用到各种设施?沙特“线性城市”来了
- 6AMD实现连续8个季度的增长 季度营收首次突破60亿美元利润更是翻倍
- 7转转集团发布2022年二季度手机行情报告:二手市场“飘香”
- 8充电宝100Wh等于多少毫安?铁路旅客禁止、限制携带和托运物品目录
- 9好消息!京东与腾讯续签三年战略合作协议 加强技术创新与供应链服务
- 10名创优品拟通过香港IPO全球发售4100万股 全球发售所得款项有什么用处?