在Python中打印表格数据
在 Python 中打印表格数据的最佳方法是什么?假设数据位于二维列表中,我想创建一个看起来很智能的表格。我实际上拥有的是一个字典列表,我想根据字典中的值打印一个交集。类似于
for val1 in my_dict:
for val2 in my_dict:
if val1['a'] > val2['a']:
print 'x'
但以这样的方式每列都是固定宽度的。 Writer 和 Formatter 类看起来似乎是可能的,但与 Perl 的格式化程序相比,使用起来仍然看起来很复杂。
是否有任何现有的实现或者我必须编写自己的实现?
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'
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将打印值“somevar”并最多使用 20 个空格。在 print 语句后面添加逗号以避免换行 - 当然:阅读 关于“%”运算符的字符串格式化操作文档
Will print the value 'somevar' and use up to 20 spaces. Add a comma behind the print statement in order to avoid the line-break - and of course: read the string formatting operations docs on the '%' operator
你知道PyPi吗?
DataGrid 和 PrettyTable 似乎是我通过简短搜索发现的两个不错的选择。在将数据发送到提供的例程之前,您可能必须按照您想要的格式组装数据(当您的条件为真时使用“x”)。
Do you know about PyPi?
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.
以下是编写正方形和立方表的两种方法:
查看此了解详细信息。
Here are two ways to write a table of squares and cubes:
Check this for details.