Python PrettyTable: add title above table title

I have a script that generates multiple tables that have the same column names and very similar data. So far, I have made each table unique by printing the title in front of it, that is:

print("Results for Method Foo") #table 1 print("Results for Method Bar") #table 2 

etc. But it is not very beautiful ..

Although this seems like a clear precedent, I could not find a way to do something like this:

PrettyTable with title

Any ideas on how I can achieve this?

Just in case, this is important: I am using python 3.4 with virtual version and excellent version 0.7.2

+7
python prettytable
source share
1 answer

This can be achieved using the PTable library, which is initially opened from PrettyTable. I did not find this in the documentation, so for others it may be useful that the syntax looks like this:

 from prettytable import PrettyTable table = PrettyTable() table.title = 'Results for method Foo' table.field_names = ['Experiment', 'Value'] table.add_row(['bla', 3.14]) table.add_row(['baz', 42.0]) print(table) 

This gives the desired result:

 +-------------------------+ | Results for method Foo | +---------------+---------+ | Experiment | Value | +---------------+---------+ | bla | 3.14 | | baz | 42.0 | +---------------+---------+ 
+3
source share

All Articles