Created
April 22, 2017 16:39
-
-
Save daragh/c98a079400093a281f242f4ae8d57a25 to your computer and use it in GitHub Desktop.
Create a printable text table string in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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