hmsaleem / prettytable

Automatically exported from code.google.com/p/prettytable
Other
0 stars 0 forks source link

Vertical format for tables which are too large to fit on the terminal #46

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Sometimes tables may become too large to fit on the terminal screen, in these 
cases, it is nice to be able to print out the table in a vertical format 
(similar to MySQL's \G option)

Here's a basic function -- would you be able to incorporate some sort of 
similar functionality in prettytable?

def format_pretty_table_vertically(table):
    '''Given a PrettyTable table instance, format each row vertically (similar to mysql's \G display)'''
    formatted = []
    max_field_width = max([len(x) for x in table._field_names])
    for row_i, row in enumerate(table._rows):
        formatted.append('*************************** %i. row ***************************' % (row_i + 1, ))
        for i, field in enumerate(table._field_names):
            formatted.append("%s: %s" % (field.rjust(max_field_width), row[i]))
    return '\n'.join(formatted)

sample output:

*************************** 1. row ***************************
People Sampled: 7294
      Location: North Pole
 Min Happiness: 1.7
 Avg Happiness: 3.7
 Max Happiness: 7.3
*************************** 2. row ***************************
People Sampled: 4321
      Location: South Pole
 Min Happiness: 3.2
 Avg Happiness: 5.2
 Max Happiness: 8.6

Thanks,
Alex

Original issue reported on code.google.com by a...@mofo.ca on 18 Dec 2013 at 6:06