Skip to content

Instantly share code, notes, and snippets.

@daragh
Created April 22, 2017 16:39
Show Gist options
  • Save daragh/c98a079400093a281f242f4ae8d57a25 to your computer and use it in GitHub Desktop.
Save daragh/c98a079400093a281f242f4ae8d57a25 to your computer and use it in GitHub Desktop.
Create a printable text table string in Python
def pretty_table(rows, sep=' | '):
rows = [[str(item) for item in row] for row in rows]
columns = zip(*rows)
widths = [max(len(item) for item in column) for column in columns]
padded = [
[item.ljust(width) for (item, width) in zip(row, widths)]
for row in rows
]
return '\n'.join(sep.join(row) for row in padded)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment