Created
March 16, 2021 20:24
-
-
Save edisongustavo/d8116e9dc41a9a509a6f2b7c7d74f299 to your computer and use it in GitHub Desktop.
Prints tables generated with python-tabulate side by side
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
from tabulate import tabulate | |
def print_tables_side_by_side(*tables, spacing: int = 3): | |
string_tables_split = [tabulate(t, headers="firstrow").splitlines() for t in tables] | |
spacing_str = " " * spacing | |
num_lines = max(map(len, string_tables_split)) | |
paddings = [max(map(len, s_lines)) for s_lines in string_tables_split] | |
for i in range(num_lines): | |
line_each_table = [] | |
for padding, table_lines in zip(paddings, string_tables_split): | |
if len(table_lines) <= i: | |
line_each_table.append(" " * (padding + spacing)) | |
else: | |
line_table_string = table_lines[i] | |
line_len = len(line_table_string) | |
line_each_table.append( | |
line_table_string + (" " * (padding - line_len)) + spacing_str | |
) | |
final_line_string = "".join(line_each_table) | |
print(final_line_string) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example:
prints: