在 Python 中打印表格数据 [英] Printing tabular data in Python

查看:39
本文介绍了在 Python 中打印表格数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Python 中打印表格数据的最佳方法是什么?假设数据在 2D 列表中,我想创建一个外观漂亮的表格.我实际拥有的是一个字典列表,我想根据字典中的值打印一个交集.类似的东西

What's the best way to print tabular data in Python? Say the data is in a 2D list and I want to create a smart looking table. What I actually have is a list of dictionaries and I want to print an intersection depending on values in the dictionaries. Something like

for val1 in my_dict:
   for val2 in my_dict:
    if val1['a'] > val2['a']:
      print 'x'

但是每列都是固定宽度的.Writter 和 formatter 类似乎是可能的领域,但与 Perl 的格式化程序相比,使用起来仍然很复杂.

but in such a way that each column is fixed width. Writter and formatter classes seem like something in the realm of possibility, but still look complex to use when compared to, say, Perl's formatter.

是否有任何现有的实现,还是我必须自己编写?

Are there any existing implementations or do I have to write my own?

推荐答案

你知道PyPi?

DataGridPrettyTable 似乎是我通过简短搜索找到的两个不错的选择.在将数据发送到提供的例程之前,您可能必须以您想要的格式组合数据(当您的条件为真时,使用x"表示).

DataGrid and PrettyTable seem like two good alternatives I found with a brief search. You may have to assemble the data in the format you want it (with "x" for when your condition is true) before sending it to the routines provided.

这篇关于在 Python 中打印表格数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆