4 Prints out a table, padded to make it pretty.
6 call pprint_table with an output (e.g. sys.stdout, cStringIO, file)
7 and table as a list of lists. Make sure table is "rectangular" -- each
8 row has the same number of columns.
13 http://ginstrom.com/scribbles/2007/09/04/pretty-printing-a-table-in-python/
15 Found with the google search "python print table" March 2010
17 Minor modifications by <peter.waller@cern.ch> to include a "-" seperator for
18 the header, and to change the number format code.
23 __author__ =
"Ryan Ginstrom"
27 from DQUtils.ext.thousands
import splitThousands
29 from six
import print_
32 """Format a number according to given places.
34 from builtins
import int
35 if isinstance(num, int):
41 """Get the maximum width of the given column index
47 """Prints out a table of data, padded for alignment
49 @param out: Output stream ("file-like object")
50 @param table: The table to print. A list of lists. Each row must have the same
57 for i
in range(len(table[0])):
60 for i, row
in enumerate(table):
62 print_(
"-" * (
sum(col_paddings) + (len(col_paddings)*3-1)), end=
'', file=out)
64 print_(row[0].ljust(col_paddings[0] + 2), end=
'', file=out)
66 for i
in range(1, len(row)):
67 col =
format_num(row[i]).rjust(col_paddings[i] + 2)
68 print_(col, end=
'', file=out)
75 from cStringIO
import StringIO
80 if __name__ ==
"__main__":
81 table = [[
"",
"taste",
"land speed",
"life"],
82 [
"spam", 300101, 4, 1003],
83 [
"eggs", 105, 13, 42],
84 [
"lumberjacks", 13, 105, 10]]