(rows)
Pretty print a table
        @param rows is a list of lists, first row assumed to be the header
Pretty print a table
Pretty print a table
def pptable(rows):
    """ Pretty print a table
        @param rows is a list of lists, first row assumed to be the header 
    """

    if len(rows) <= 0:
        return

    # Convert items to strings
    new_rows = []
    for i in rows:
        new_rows.append([str(r) for r in i])
    rows = new_rows

    # Add square brackets to numbers in left row
    for i in rows:
        try:
            if int(i[0]):
                i[0] = "[" + i[0] + "]"
        except:
            pass

    # Determine max length of columns
    lens = []
    headers = rows[0]
    for i in range(len(rows[0])):
        lens.append(len(str(max([x[i] for x in rows] + [headers[i]],
            key = lambda x: len(str(x))))))

    # Add spacing
    repack = []
    for row in rows:
        new_row = []
        for size,data in zip(lens,row):
            data = str(data)
            if len(data) < size:
                new_row.append(data + ((size - len(data)) * " "))
            else:
                new_row.append(data)
        repack.append(new_row)

    # Add color
    required_flag = False
    first_line = True
    if "Required" in repack[0]: required_flag = True
    added_colors = []
    for row in repack:
        new_line = []
        for i in row:
            if first_line:
                new_line.append(color.B_YELLOW + i + color.END)
            else:
                if required_flag and ("True" in i or "False" in i):
                    if "False" in i:
                        new_line.append(color.B_WHITE + i + color.END)
                    else:
                        new_line.append(color.B_CYAN + i + color.END)
                elif '[' in i or ']' in i:
                    i = i.replace('[', color.B_GREEN + '[' + color.B_YELLOW)
                    i = i.replace(']', color.B_GREEN + ']' + color.B_WHITE)
                    i = i + color.END
                    new_line.append(i)
                else:
                    new_line.append(color.B_WHITE + i + color.END)
        first_line = False
        added_colors.append(new_line)

    # Create spacing string
    space_string = '+-'
    for i in lens:
        space_string = space_string + ("-" * i) + "-+-"
    space_string = '\t' + space_string

    # Display glorious table
    print space_string
    for row in added_colors:
        first = True
        for i in row:
            if first is True:
                sys.stdout.write("\t| " + str(i))
                first = False
            else:
                sys.stdout.write(" | " + str(i))
        print ' | '
        print space_string
def pptable ( rows ) : if len ( rows ) <= 0 : return # Convert items to strings new_rows = [ ] for i in rows : new_rows . append ( [ str ( r ) for r in i ] ) rows = new_rows # Add square brackets to numbers in left row for i in rows : try : if int ( i [ 0 ] ) : i [ 0 ] = "[" + i [ 0 ] + "]" except : pass # Determine max length of columns lens = [ ] headers = rows [ 0 ] for i in range ( len ( rows [ 0 ] ) ) : lens . append ( len ( str ( max ( [ x [ i ] for x in rows ] + [ headers [ i ] ] , key = lambda x : len ( str ( x ) ) ) ) ) ) # Add spacing repack = [ ] for row in rows : new_row = [ ] for size , data in zip ( lens , row ) : data = str ( data ) if len ( data ) < size : new_row . append ( data + ( ( size - len ( data ) ) * " " ) ) else : new_row . append ( data ) repack . append ( new_row ) # Add color required_flag = False first_line = True if "Required" in repack [ 0 ] : required_flag = True added_colors = [ ] for row in repack : new_line = [ ] for i in row : if first_line : new_line . append ( color . B_YELLOW + i + color . END ) else : if required_flag and ( "True" in i or "False" in i ) : if "False" in i : new_line . append ( color . B_WHITE + i + color . END ) else : new_line . append ( color . B_CYAN + i + color . END ) elif '[' in i or ']' in i : i = i . replace ( '[' , color . B_GREEN + '[' + color . B_YELLOW ) i = i . replace ( ']' , color . B_GREEN + ']' + color . B_WHITE ) i = i + color . END new_line . append ( i ) else : new_line . append ( color . B_WHITE + i + color . END ) first_line = False added_colors . append ( new_line ) # Create spacing string space_string = '+-' for i in lens : space_string = space_string + ( "-" * i ) + "-+-" space_string = '\t' + space_string # Display glorious table print space_string for row in added_colors : first = True for i in row : if first is True : sys . stdout . write ( "\t| " + str ( i ) ) first = False else : sys . stdout . write ( " | " + str ( i ) ) print ' | ' print space_string