def print_table(table, row_names, col_names, latex_file = None):
"""Pretty prints the table given the table, and row and col names.
If a latex_file is provided (and tabulate is installed), it also writes a
file containing the LaTeX source of the table (which you can \input into your report)
"""
try:
from tabulate import tabulate
rows = map(lambda r,t: [r] + t, zip(row_names,table.tolist()))
print(tabulate(rows, headers = [""] + col_names))
if latex_file is not None:
latex_str = tabulate(rows, headers = [""] + col_names, tablefmt="latex")
with open(latex_file, 'w') as f:
f.write(latex_str)
f.close()
except ImportError as e:
row_format ="{:>15} " * (len(col_names) + 1)
print(row_format.format("", *col_names))
for row_name, row in zip(row_names, table):
print(row_format.format(row_name, *row))
评论列表
文章目录