def tabulate(table, wrap_column=0, output=sys.stdout):
 column_widths = [get_max_width(table, i) for i in range(len(table[0]))]
 column_widths[(-1)] = len(table[0][(-1)])
 if ((wrap_column is not None) and (wrap_column > 0)):
  column_widths = [(width if (width <= wrap_column) else wrap_column) for width in column_widths]
 headers = table.pop(0)
 for i in range(len(headers)):
  padding = (2 if (i < (len(headers) - 1)) else 0)
  output.write(unicode(headers[i]).ljust((column_widths[i] + padding)))
 output.write(u'\n')
 for i in range(len(headers)):
  padding = (2 if (i < (len(headers) - 1)) else 0)
  if headers[i]:
   output.write((u'-' * len(headers[i])).ljust((column_widths[i] + padding)))
  else:
   output.write(u''.ljust((column_widths[i] + padding)))
 output.write(u'\n')
 added_row = False
 for (j, row) in enumerate(table):
  for i in range(len(row)):
   padding = (2 if (i < (len(row) - 1)) else 0)
   column = unicode(row[i])
   if ((wrap_column is not None) and (wrap_column != 0) and (len(column) > wrap_column)):
    wrapped = textwrap.wrap(column, wrap_column)
    column = wrapped.pop(0)
    lines = u''.join(wrapped)
    if added_row:
     table[(j + 1)][i] = lines
    else:
     table.insert((j + 1), ([u''] * len(row)))
     table[(j + 1)][i] = lines
     added_row = True
   output.write(column.ljust((column_widths[i] + padding)))
  added_row = False
  output.write(u'\n')
 output.write(u'\n')

def tabulate ( table , wrap_column = 0 , output = sys . stdout ) : column_widths = [ get_max_width ( table , i ) for i in range ( len ( table [ 0 ] ) ) ] column_widths [ ( - 1 ) ] = len ( table [ 0 ] [ ( - 1 ) ] ) if ( ( wrap_column is not None ) and ( wrap_column > 0 ) ) : column_widths = [ ( width if ( width <= wrap_column ) else wrap_column ) for width in column_widths ] headers = table . pop ( 0 ) for i in range ( len ( headers ) ) : padding = ( 2 if ( i < ( len ( headers ) - 1 ) ) else 0 ) output . write ( unicode ( headers [ i ] ) . ljust ( ( column_widths [ i ] + padding ) ) ) output . write ( u'\n' ) for i in range ( len ( headers ) ) : padding = ( 2 if ( i < ( len ( headers ) - 1 ) ) else 0 ) if headers [ i ] : output . write ( ( u'-' * len ( headers [ i ] ) ) . ljust ( ( column_widths [ i ] + padding ) ) ) else : output . write ( u'' . ljust ( ( column_widths [ i ] + padding ) ) ) output . write ( u'\n' ) added_row = False for ( j , row ) in enumerate ( table ) : for i in range ( len ( row ) ) : padding = ( 2 if ( i < ( len ( row ) - 1 ) ) else 0 ) column = unicode ( row [ i ] ) if ( ( wrap_column is not None ) and ( wrap_column != 0 ) and ( len ( column ) > wrap_column ) ) : wrapped = textwrap . wrap ( column , wrap_column ) column = wrapped . pop ( 0 ) lines = u'' . join ( wrapped ) if added_row : table [ ( j + 1 ) ] [ i ] = lines else : table . insert ( ( j + 1 ) , ( [ u'' ] * len ( row ) ) ) table [ ( j + 1 ) ] [ i ] = lines added_row = True output . write ( column . ljust ( ( column_widths [ i ] + padding ) ) ) added_row = False output . write ( u'\n' ) output . write ( u'\n' )
tabulate -> iterator arguments: func: the function to tabulate over .