0

I am trying to print a table inside a border. As you can see, without the border, the table prints fine, but when I put a border, it just does not print properly. Please help me accomplish this. If there is any other easy way, please suggest.

import tabulate
from textwrap import wrap
import prettytable as pt

apps_iter_data = [
    {"hello1": "hello1_data","hello2": "hello1_data","hello3": "hello1_data","hello4": "hello1_data","hello5": "hello1_data","hello6": "hello1_data"},
    {"hello1": "hello1_data","hello2": "hello1_data","hello3": "hello1_data","hello4": "hello1_data","hello5": "hello1_data","hello6": "hello1_data"},
    {"hello1": "hello1_data","hello2": "hello1_data","hello3": "hello1_data","hello4": "hello1_data","hello5": "hello1_data","hello6": "hello1_data"},
    {"hello1": "hello1_data","hello2": "hello1_data","hello3": "hello1_data","hello4": "hello1_data","hello5": "hello1_data","hello6": "hello1_data"},
    {"hello1": "hello1_data","hello2": "hello1_data","hello3": "hello1_data","hello4": "hello1_data","hello5": "hello1_data","hello6": "hello1_data"},
    {"hello1": "hello1_data","hello2": "hello1_data","hello3": "hello1_data","hello4": "hello1_data","hello5": "hello1_data","hello6": "hello1_data"},
    {"hello1": "hello1_data","hello2": "hello1_data","hello3": "hello1_data","hello4": "hello1_data","hello5": "hello1_data","hello6": "hello1_data"}
]
message_type = "info"
header = apps_iter_data[0].keys()
rows =  [x.values() for x in apps_iter_data]
message = tabulate.tabulate(rows, header)
print(message)

width = len(message.splitlines()[1])
t = pt.PrettyTable()
t.field_names = [message_type.upper()]
[t.add_row([message[i:i + width]]) for i in range(0, len(message), width)]

print(t)



hello1       hello2       hello3       hello4       hello5       hello6
-----------  -----------  -----------  -----------  -----------  -----------
hello1_data  hello1_data  hello1_data  hello1_data  hello1_data  hello1_data
hello1_data  hello1_data  hello1_data  hello1_data  hello1_data  hello1_data
hello1_data  hello1_data  hello1_data  hello1_data  hello1_data  hello1_data
hello1_data  hello1_data  hello1_data  hello1_data  hello1_data  hello1_data
hello1_data  hello1_data  hello1_data  hello1_data  hello1_data  hello1_data
hello1_data  hello1_data  hello1_data  hello1_data  hello1_data  hello1_data
hello1_data  hello1_data  hello1_data  hello1_data  hello1_data  hello1_data
+------------------------------------------------------------------------------+
|                                     INFO                                     |
+------------------------------------------------------------------------------+
|   hello1       hello2       hello3       hello4       hello5       hello6    |
|                                     ----                                     |
|   -------  -----------  -----------  -----------  -----------  -----------   |
|                                     hel                                      |
|  lo1_data  hello1_data  hello1_data  hello1_data  hello1_data  hello1_data   |
|                                      he                                      |
|  llo1_data  hello1_data  hello1_data  hello1_data  hello1_data  hello1_data  |
|                                      h                                       |
| ello1_data  hello1_data  hello1_data  hello1_data  hello1_data  hello1_data  |
|                                                                              |
| hello1_data  hello1_data  hello1_data  hello1_data  hello1_data  hello1_data |
|                                                                              |
| hello1_data  hello1_data  hello1_data  hello1_data  hello1_data  hello1_dat  |
|                                      a                                       |
|  hello1_data  hello1_data  hello1_data  hello1_data  hello1_data  hello1_da  |
|                                      ta                                      |
|  hello1_data  hello1_data  hello1_data  hello1_data  hello1_data  hello1_d   |
|                                     ata                                      |
+------------------------------------------------------------------------------+
Akshay J
  • 5,362
  • 13
  • 68
  • 105

2 Answers2

1

The other easy way, IMHO, is using the rich package. This is drawn straight from the documentation:

from rich.console import Console
from rich.table import Table

table = Table(title="Star Wars Movies")

table.add_column("Released", justify="right", style="cyan", no_wrap=True)
table.add_column("Title", style="magenta")
table.add_column("Box Office", justify="right", style="green")

table.add_row("Dec 20, 2019", "Star Wars: The Rise of Skywalker", "$952,110,690")
table.add_row("May 25, 2018", "Solo: A Star Wars Story", "$393,151,347")
table.add_row("Dec 15, 2017", "Star Wars Ep. V111: The Last Jedi", "$1,332,539,889")
table.add_row("Dec 16, 2016", "Rogue One: A Star Wars Story", "$1,332,439,889")

console = Console()
console.print(table)

enter image description here

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
1

I never used the prettytable module before, but by splitting your list of dictionaries in another way, I manage to present the table cleanly as you want.

t = pt.PrettyTable([x for x in apps_iter_data[0].keys()])

for i in apps_iter_data:
  row = list(i.values())
  t.add_row(row)
print(t)

Output:

+-------------+-------------+-------------+-------------+-------------+-------------+
|    hello1   |    hello2   |    hello3   |    hello4   |    hello5   |    hello6   |
+-------------+-------------+-------------+-------------+-------------+-------------+
| hello1_data | hello1_data | hello1_data | hello1_data | hello1_data | hello1_data |
| hello1_data | hello1_data | hello1_data | hello1_data | hello1_data | hello1_data |
| hello1_data | hello1_data | hello1_data | hello1_data | hello1_data | hello1_data |
| hello1_data | hello1_data | hello1_data | hello1_data | hello1_data | hello1_data |
| hello1_data | hello1_data | hello1_data | hello1_data | hello1_data | hello1_data |
| hello1_data | hello1_data | hello1_data | hello1_data | hello1_data | hello1_data |
| hello1_data | hello1_data | hello1_data | hello1_data | hello1_data | hello1_data |
+-------------+-------------+-------------+-------------+-------------+-------------+

It should be possible to further optimize my sample code I think.

Laurent
  • 116
  • 1
  • 8