Search code examples
pythonprettytable

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


I’m having a problem printing a table with terminaltables.

Here's my main script:

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

and here's the configuration file 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

This print me a table for each entry like this:

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

but what I want is this:

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

How can I modify the script to get this output?


Solution

  • The problem is that you recreate table_data and table on each iteration of the loop. You print on each iteration, and then the old data gets thrown away and a new table gets started from scratch. There’s no overlap in the body of the tables you’re creating.

    You should have a single table_data, which starts with the headings, then you gather all the table data before printing anything. Add the new entries on each iteration of the loop, and put the print statement after the for loop is finished. Here’s an example:

    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
    

    and here’s what it outputs:

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

    If you want to have a horizontal rule between the bug_tracker and wiki lines, then you need to set table.inner_row_border to True. So you’d replace the last two lines with:

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