我可以将prettytable创建的输出格式写入文件吗?

时间:2014-04-29 06:30:33

标签: python python-3.x

prettytable.PrettyTable可以表示视觉上吸引人的格式化表格中的表格数据,并将这些表格打印到终端。

如何将表格保存为文本文件?

1 个答案:

答案 0 :(得分:10)

根据tutorial section on ASCII tables,您可以将表格输出作为字符串:

table = ... # code for creating table goes here
table_txt = table.get_string()
with open('output.txt','w') as file:
    file.write(table_txt)