首页 > 解决方案 > 非常具体的输出格式 Python

问题描述

我想弄清楚如何在终端中具体打印如下:

桌子

然后将其写入 .txt 文件。

我的尝试:

我使用“制表”已经有一段时间了,所以我自然而然地尝试使用这个库。这是我的代码:

    from tabulate import tabulate
    import numpy as np

    lista  = np.zeros(4)
    print(tabulate([lista, lista], headers=['n', r'$\phi_{n}$', 'a_{n}', 'e_{n}'], 
    numalign="center"))

    with open('table.txt', 'w') as f:
        f.write(tabulate([lista, lista], headers=['n', r'$\phi_{n}$', 'a_{n}', 'e_{n}'], numalign="center"))

上面的代码生成以下结果:

生成的图像

这很好,但不是我想要的。我试图删除“标题”参数,但它仍然给了我一个包含标题结构的表。此外,它不包含我需要的 '&' 字符和 '\' 的东西。我怀疑我可能需要以某种方式手动完成。

在此先感谢,卢卡斯

标签: python-3.xformattingtabular

解决方案


推荐阅读