DECROMAX

Print table / grid

Oct 18th, 2021
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.41 KB | None | 0 0
  1. def print_table(data, cols, wide):
  2.     """Prints formatted data on columns of given width. useful for printing grid from list
  3.    param - cols: (how many cols wide) wide: how much padding"""
  4.     n, r = divmod(len(data), cols)
  5.     pat = '{{:{}}}'.format(wide)
  6.     line = '\n'.join(pat * cols for _ in range(n))
  7.     last_line = pat * r
  8.     print(line.format(*data))
  9.     print(last_line.format(*data[n * cols:]))
Advertisement
Add Comment
Please, Sign In to add comment