10 examples of 'printing table in python' in Python

Every line of 'printing table in python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
137def pprint_table(out, table):
138 """Prints out a table of data, padded for alignment
139 @param out: Output stream (file-like object)
140 @param table: The table to print. A list of lists.
141 Each row must have the same number of columns. """
142 col_paddings = []
143
144
145 for i in range(len(table[0])):
146 col_paddings.append(get_max_width(table, i))
147
148 for row in table:
149 # left col
150 print >> out, row[0].ljust(col_paddings[0] + 1),
151 # rest of the cols
152 for i in range(1, len(row)):
153 col = format_num(row[i]).rjust(col_paddings[i] + 2)
154 print >> out, col,
155 print >> out
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

42def pprint_table(out, table):
43 """Prints out a table of data, padded for alignment
44
45 @param out: Output stream ("file-like object")
46 @param table: The table to print. A list of lists. Each row must have the same
47 number of columns.
48
49 """
50
51 col_paddings = []
52
53 for i in range(len(table[0])):
54 col_paddings.append(get_max_width(table, i))
55
56 for row in table:
57 # left col
58 print >> out, row[0].ljust(col_paddings[0] + 1),
59 # rest of the cols
60 for i in range(1, len(row)):
61 col = format_num(row[i]).rjust(col_paddings[i] + 2)
62 print >> out, col,
63 print >> out
42def print_table(table: List[List[str]]):
43 """ Print the lists with evenly spaced columns """
44
45 # print while padding each column to the max column length
46 col_lens = [0] * len(table[0])
47 for row in table:
48 for i,cell in enumerate(row):
49 col_lens[i] = max(len(cell), col_lens[i])
50
51 formats = ["{0:<%d}" % x for x in col_lens]
52 for row in table:
53 print(" ".join(formats[i].format(row[i]) for i in range(len(row))))
86def __prints(table):
87 node = tables[table]
88 __do_print(node)
462def print_list(objs, fields, formatters={}):
463 pt = prettytable.PrettyTable([f for f in fields], caching=False)
464 pt.aligns = ['l' for f in fields]
465
466 for o in objs:
467 row = []
468 for field in fields:
469 if field in formatters:
470 row.append(formatters[field](o))
471 else:
472 row.append(getattr(o, field.lower().replace(' ', '_'), ''))
473 pt.add_row(row)
474
475 pt.printt(sortby=fields[0])
232def table_print(t, a, d):
233 outfile = "Das_t_%s.kef" % d
234 print outfile
235 fh = open(outfile, 'w')
236 i = 0
237 # Loop through table rows
238 for r in a.rows:
239 i += 1
240 fh.write("# Table row %d\n" % i)
241 # Print table name
242 fh.write("%s\n" % t)
243 # Loop through each row column and print
244 for k in a.keys:
245 fh.write("\t%s = %s\n" % (k, str(r[k])))
246
247 fh.close()
13def print_table(table):
14 col_width = [min(max(len(x) for x in col), 35) for col in zip(*table)]
15 # print(col_width)
16 for idx, line in enumerate(table):
17 if idx == 1: # first line after table head
18 print("-" * (sum(col_width) + 3 * len(col_width) + 1))
19 print(
20 "| "
21 + " | ".join("{:{}}".format(str(x), col_width[i]) for i, x in enumerate(line))
22 + " |"
23 )
49def makeTable(values, height, width):
50 lines = [[] for i in range(height + 1)]
51 firstRowString = "|{}" + "+{}" * (len(values) - 1) + '|'
52 rowString = "|{}" * len(values) + '|'
53 cWidth = (width // len(values)) - 1
54 cRemainder = width % len(values) - 1
55 for title, rows in values.items():
56 if cRemainder > 0:
57 heading = "{1:-^{0}}".format(cWidth + 1, title)
58 cRemainder -= 1
59 elif cRemainder < 0:
60 heading = "{1:-^{0}}".format(cWidth - 1, title)
61 cRemainder += 1
62 else:
63 heading = "{1:-^{0}}".format(cWidth, title)
64 hWidth = len(heading)
65 lines[0].append(heading)
66 if len(rows) < height:
67 for i in range(height - len(rows)):
68 rows.append(('NA', -1))
69 for index, entry in enumerate((prepEntry(hWidth, *s) for s in rows[:height]), start = 1):
70 lines[index].append(entry)
71 retLines = []
72 retLines.append(firstRowString.format(*tuple(lines[0])))
73 for line in lines[1:]:
74 retLines.append(rowString.format(*tuple(line)))
75 return '\n'.join(retLines)
102def print_table(table, colsep=' ', rowsep='\n', align=None, out=sys.stdout):
103 """Print a 2D rectangular array, aligning columns with spaces.
104
105 Args:
106 align: Optional string of 'l' and 'r', designating whether each column is
107 left- or right-aligned. Defaults to left aligned.
108 """
109 if len(table) == 0:
110 return
111
112 colwidths = None
113 for row in table:
114 if colwidths is None:
115 colwidths = [num_codepoints(x) for x in row]
116 else:
117 colwidths = [max(colwidths[i], num_codepoints(x))
118 for i, x in enumerate(row)]
119
120 if align is None: # pragma: no cover
121 align = 'l' * len(colwidths)
122
123 for row in table:
124 cells = []
125 for i, cell in enumerate(row):
126 padding = ' ' * (colwidths[i] - num_codepoints(cell))
127 if align[i] == 'r':
128 cell = padding + cell
129 elif i < len(row) - 1:
130 # Do not pad the final column if left-aligned.
131 cell += padding
132 cells.append(cell)
133 try:
134 print(*cells, sep=colsep, end=rowsep, file=out)
135 except IOError: # pragma: no cover
136 # Can happen on Windows if the pipe is closed early.
137 pass
295def twPrint(obj):
296 """
297 A simple caller of twClosure (see docstring for twClosure)
298 """
299 twPrinter = twClosure()
300 print(twPrinter(obj))

Related snippets