Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save brokeDude2901/018c3fb2cf7343e9c29c3e6855accb68 to your computer and use it in GitHub Desktop.
Save brokeDude2901/018c3fb2cf7343e9c29c3e6855accb68 to your computer and use it in GitHub Desktop.
Prints tables generated with python-tabulate side by side
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