data.py 文件源码

python
阅读 26 收藏 0 点赞 0 评论 0

项目:uci-statnlp 作者: sameersingh 项目源码 文件源码
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))
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号