最优美的表格查看插件:tabulate

在平常的数据分析过程中,python本身的list、dict等数据直接打印出来如果数据较小还可以明显的查看,若是数据量稍微有个几十行的话找起来简直是眼花缭乱的。

【阅读全文】

最优美的表格查看插件:tabulate

安装没有什么特别的,直接安装就可以了。

[En]

There is nothing special about the installation, just install it directly.

pip install tabulate

它也相对容易使用,首先将扩展库导入到我们的代码块中。

[En]

It is also relatively easy to use, first import the extension library into our code block.

from tabulate import tabulate

创建一个由两维数组组成的表来查看结果!

[En]

Create a table of two-dimensional arrays to see the results!

list_ = [['张三', '90班', '98'], ['张三', '90班', '98'], ['张三', '90班', '98'], ['张三', '90班', '98'], ['张三', '90班', '98']]

print(tabulate(list_))

----  ----  --
张三  90班  98
张三  90班  98
张三  90班  98
张三  90班  98
张三  90班  98
----  ----  --

看看这效果还是比较nice的,结果一目了然。

如果想要更清晰,可以使用光栅化显示模式进行打印。

[En]

If you want to be clearer, you can use a rasterized display mode to print.

print(tabulate(list_, tablefmt='grid'))

+------+------+----+
| 张三 | 90班 | 98 |
+------+------+----+
| 张三 | 90班 | 98 |
+------+------+----+
| 张三 | 90班 | 98 |
+------+------+----+
| 张三 | 90班 | 98 |
+------+------+----+
| 张三 | 90班 | 98 |
+------+------+----+

例如,还有另一个二维数据,其中第一条数据应该是标题的一部分,需要单独显示,以便您可以看到它是标题。

[En]

For example, there is another two-dimensional data, in which the first piece of data should be the part of the header, which needs to be displayed separately so that you can see that it is the header.

设置第一行作为表头。

list_ = [['姓名', '班级', '成绩'], ['张三', '90班', '98'], ['张三', '90班', '98'], ['张三', '90班', '98']]

print(tabulate(list_, headers='firstrow'))

姓名    班级      成绩
------  ------  ------
张三    90班        98
张三    90班        98
张三    90班        98

同样的,dict的字典类型的数据也是可以很方便的打印的。

dict_ = {
    '姓名': ['张三', '李四'],
    '年龄': ['29', '25']
}

print(tabulate(dict_,headers='keys'))

姓名      年龄
------  ------
张三        29
李四        25

我是 [Python 集中营]、很高兴您看到了最后, 我是一个专注于 Python 知识分享的公众号,希望可以得到您的关注~

最优美的表格查看插件:tabulate

【往期精彩】

抖音同款课堂点名系统,PyQt5写起来很简单…

开工啦!批量向PDF文件添加中文水印…

大年初二、做了一个windows通知管理器!

百度图片下载器2.0

gif动态图片生成器,多张图片组合后生成动图…

Original: https://www.cnblogs.com/lwsbc/p/15877244.html
Author: Python集中营
Title: 最优美的表格查看插件:tabulate

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/510102/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球