0

I was trying to display the title in prettyTable but not sure what is wrong here -

#!/usr/bin/python3
from prettytable import PrettyTable
hostname = "abc.com"
table = PrettyTable()
table.title = hostname
table.field_names = ['hostname', 'process']
table.add_row(['abc.com', '31324'])
table.add_row(['abc.com', '42230'])
print(table)

Output -

+----------+---------+
| hostname | process |
+----------+---------+
| abc.com  |  31324  |
| abc.com  |  42230  |
+----------+---------+

Expected Output -

+----------+---------+
|       abc.com      |
+----------+---------+
| hostname | process |
+----------+---------+
| abc.com  |  31324  |
| abc.com  |  42230  |
+----------+---------+
Anoop R Desai
  • 712
  • 5
  • 18
VIPIN KUMAR
  • 3,019
  • 1
  • 23
  • 34
  • 1
    Possible duplicate of [Python PrettyTable: Add title above the table's header](https://stackoverflow.com/a/46434781/1898437). – Anoop R Desai Sep 02 '19 at 06:43

2 Answers2

2

The issue is that you are using the prettytable package (c. 2013) and not the PTable package (c. 2015). Both provide the prettytable module. Only the PTable package supports table titles.

Dan D.
  • 73,243
  • 15
  • 104
  • 123
1

according to this doc this should work (not tested):

print(table.get_string(title="abc.com"))
hiro protagonist
  • 44,693
  • 14
  • 86
  • 111