Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
GoogleCodeExporter opened this issue Sep 1, 2015 · 0 comments

Comments

@GoogleCodeExporter
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant