使用终端表,我怎样才能将所有数据放在一个表中,而不是拆分到多个表中?

Using terminaltables, how can I get all my data in a single table, rather than split across multiple tables?

我在使用终端 table 打印 table 时遇到问题。

这是我的主要脚本:

from ConfigParser import SafeConfigParser
from terminaltables import AsciiTable

parser = SafeConfigParser()
parser.read('my.conf')

for section_name in parser.sections():
    description = parser.get(section_name,'description')
    url = parser.get(section_name,'url')
    table_data = [['Repository', 'Url', 'Description'], [section_name, url, description]]
    table = AsciiTable(table_data)
    print table.table

这是配置文件 my.conf:

[bug_tracker]
description = some text here
url = http://localhost.tld:8080/bugs/ 
username = dhellmann
password = SECRET

[wiki] 
description = foo bar bla
url = http://localhost.tld:8080/wiki/
username = dhellmann
password = SECRET

这会为每个条目打印一个 table,如下所示:

+-------------+---------------------------------+------------------------+
| Repository  | Url                             | Description            |
+-------------+---------------------------------+------------------------+
| bug_tracker | http://localhost.foo:8080/bugs/ | some text here         |
+-------------+---------------------------------+------------------------+
+------------+---------------------------------+-------------+
| Repository | Url                             | Description |
+------------+---------------------------------+-------------+
| wiki       | http://localhost.foo:8080/wiki/ | foo bar bla |
+------------+---------------------------------+-------------+

但我想要的是:

+-------------+---------------------------------+------------------------+
| Repository  | Url                             | Description            |
+-------------+---------------------------------+------------------------+
| bug_tracker | http://localhost.foo:8080/bugs/ | some text here         |
+-------------+---------------------------------+------------------------+
| wiki        | http://localhost.foo:8080/wiki/ | foo bar bla            |
+-------------+---------------------------------+------------------------+

如何修改脚本以获得此输出?

问题是您在循环的每次迭代中重新创建 table_datatable。您在每次迭代时打印,然后旧数据被丢弃,新的 table 从头开始​​。您正在创建的 table 的 body 没有重叠。

您应该有一个 table_data,它以标题开头,然后在 打印任何内容之前 收集所有 table 数据。在循环的每次迭代中添加新条目,并在 for 循环完成后放置打印语句。这是一个例子:

from ConfigParser import SafeConfigParser
from terminaltables import AsciiTable

parser = SafeConfigParser()
parser.read('my.conf')

table_data = [['Repository', 'Url', 'Description']]

for section_name in parser.sections():
    description = parser.get(section_name,'description')
    url = parser.get(section_name,'url')
    table_data.append([section_name, url, description])

table = AsciiTable(table_data)
print table.table

这是它的输出:

+-------------+---------------------------------+----------------+
| Repository  | Url                             | Description    |
+-------------+---------------------------------+----------------+
| bug_tracker | http://localhost.tld:8080/bugs/ | some text here |
| wiki        | http://localhost.tld:8080/wiki/ | foo bar bla    |
+-------------+---------------------------------+----------------+

如果你想在 bug_tracker 和 wiki 行之间有一条水平线,那么你需要将 table.inner_row_border 设置为 True。所以你将最后两行替换为:

table = AsciiTable(table_data)
table.inner_row_border = True
print table.table