【问题标题】:Using terminaltables, how can I get all my data in a single table, rather than split across multiple tables?使用终端表,我怎样才能将所有数据放在一个表中,而不是拆分到多个表中?
【发布时间】:2015-03-01 17:16:48
【问题描述】:

我在打印带有终端表的表格时遇到问题。

这是我的主要脚本:

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

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

+-------------+---------------------------------+------------------------+
| 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            |
+-------------+---------------------------------+------------------------+

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

【问题讨论】:

  • 您每次都在创建一个新表,您想解析每个条目的部分数据,然后创建该单个表。

标签: python prettytable


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-22
    • 2022-12-07
    • 2020-07-24
    • 2023-02-09
    • 1970-01-01
    • 1970-01-01
    • 2011-07-18
    • 1970-01-01
    相关资源
    最近更新 更多