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