We don’t allow questions seeking recommendations for software libraries, tutorials, tools, books, or other off-site resources. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
The community reviewed whether to reopen this question 2 years ago and left it closed:
Original close reason(s) were not resolved
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(13)
我很久以前就读过这个问题,并完成了我自己的表格漂亮打印机的编写:
制表
。我的用例是:
给定您的示例,网格可能是最重要的类似的输出格式:
其他支持的格式有
plain
(无行)、simple
(Pandoc 简单表格)、pipe
(如 PHP Markdown Extra 中的表格) )、orgtbl
(如 Emacs org-mode 中的表格)、rst
(如 reStructuredText 中的简单表格)。grid
和orgtbl
可以在 Emacs 中轻松编辑。在性能方面,
tabulate
比asciitable
稍慢,但比PrettyTable
和texttable
快得多。PS 我也非常喜欢按小数对齐数字 列。因此,如果有的话,这是数字的默认对齐方式(可覆盖)。
I've read this question long time ago, and finished writing my own pretty-printer for tables:
tabulate
.My use case is:
Given your example,
grid
is probably the most similar output format:Other supported formats are
plain
(no lines),simple
(Pandoc simple tables),pipe
(like tables in PHP Markdown Extra),orgtbl
(like tables in Emacs' org-mode),rst
(like simple tables in reStructuredText).grid
andorgtbl
are easily editable in Emacs.Performance-wise,
tabulate
is slightly slower thanasciitable
, but much faster thanPrettyTable
andtexttable
.P.S. I'm also a big fan of aligning numbers by a decimal column. So this is the default alignment for numbers if there are any (overridable).
这是我编写的一个快速但肮脏的小函数,用于显示只能通过 SOAP API 进行的 SQL 查询的结果。它需要输入一系列一个或多个
namedtuples
作为表行。如果只有一条记录,则会以不同的方式打印出来。它对我来说很方便,可以作为您的起点:
示例输出:
示例
Here's a quick and dirty little function I wrote for displaying the results from SQL queries I can only make over a SOAP API. It expects an input of a sequence of one or more
namedtuples
as table rows. If there's only one record, it prints it out differently.It is handy for me and could be a starting point for you:
Sample output:
Example
由于某种原因,当我在谷歌搜索中包含“docutils”时,我偶然发现了 texttable,这似乎是我在寻找什么。
For some reason when I included 'docutils' in my google searches I stumbled across texttable, which seems to be what I'm looking for.
我也为此写了自己的解决方案。我试图保持简单。
https://github.com/Robpol86/terminaltables
I too wrote my own solution to this. I tried to keep it simple.
https://github.com/Robpol86/terminaltables
为此,我刚刚发布了 termtables。例如,
默认
情况下,表格使用 Unicode 方框绘制字符 呈现,
术语表非常可配置;查看测试以获取更多示例。
I just released termtables for this purpose. For example, this
gets you
By default, the table is rendered with Unicode box-drawing characters,
termtables are very configurable; check out the tests for more examples.
如果您想要一个具有列和行跨度的表格,请尝试我的库 dashtable
其输出:
If you want a table with column and row spans, then try my library dashtable
Which outputs:
使用 w3m 的版本旨在处理 MattH 版本接受的类型:
结果:
Version using w3m designed to handle the types MattH's version accepts:
results in:
您可以尝试 BeautifulTable。它做你想做的事。这是文档中的一个示例
You can try BeautifulTable. It does what you want to do. Here's an example from it's documentation
示例:
输出:
Example:
Output:
我知道这个问题有点老了,但这是我的尝试:
https://gist.github.com /lonetwin/4721748
恕我直言,它更具可读性(尽管它不像 @MattH 的解决方案那样区分单行/多行,也不使用 NamedTuples)。
I know it the question is a bit old but here's my attempt at this:
https://gist.github.com/lonetwin/4721748
It is a bit more readable IMHO (although it doesn't differentiate between single / multiple rows like @MattH's solutions does, nor does it use NamedTuples).
这是我的解决方案:
Here's my solution:
我使用这个小实用函数。
输出
I use this small utility function.
output
这可以仅使用内置模块相当紧凑地使用列表和字符串理解来完成。接受格式相同的字典列表...
This can be done with only builtin modules fairly compactly using list and string comprehensions. Accepts a list of dictionaries all of the same format...