Created
July 28, 2023 10:24
-
-
Save jelmervdl/3b45af92aaf91b75c3040a7f2824b973 to your computer and use it in GitHub Desktop.
This file contains 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 print_cells(*rows, format:Callable[[Any],str]=repr,file=None) -> None: | |
# Convert it all to text | |
text_rows = [ | |
[format(cell) for cell in row] | |
for row in rows | |
] | |
# Calculate column width | |
widths = [ | |
max(len(cell) for cell in column) | |
for column in zip(*text_rows) | |
] | |
# Print rows | |
for row in text_rows: | |
for width, cell in zip(widths, row): | |
print(f'{{:<{width:d}}} '.format(cell), end='', file=file) | |
print(file=file) | |
print_cells([(1,2), (3,4), (5,6)]) | |
# prints | |
# 1 3 5 | |
# 2 4 6 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do yourself (myself, once I read this in the future) and use https://pypi.org/project/wcwidth/.