view profilemanager/utils.py @ 70:6e282cacd5f0

split the string
author Jeff Hammel <jhammel@mozilla.com>
date Fri, 07 May 2010 16:06:05 -0700
parents ca57920aa223
children 145e111903d2
line wrap: on
line source

#!/usr/bin/env python

def format_tabular(table, spacer=' '):
    columns = [0 for i in range(max([len(row) for row in table]))]
    assert (len(columns) == min([len(row) for row in table]))
    for row in table:
        for index, column in enumerate(row):
            if len(column) > columns[index]:
                columns[index] = len(column)
    format_string = spacer.join(['%s%s' for i in range(len(columns))])
    retval = []
    for row in table:
        values = []
        for index, column in enumerate(row):
            values.append(column)
            values.append(' ' * (columns[index] - len(column)))
        retval.append(format_string % tuple(values))
    return '\n'.join(retval)
    
if __name__ == '__main__':
    test_data = [ ['fox', 'i am a fox!', 'Foxkeh'],
                  ['cat', 'meow', 'Lilly'],
                  ['elephant', 'elephants shower with their trunks', 'Elephino'] ]
    print format_tabular(test_data)