Skip to main content
 首页 » 编程设计

python之在 python 中的输出文件中编写 prettytable

2025年02月15日8JustinYoung

我正在解析一个文本文件并将我需要的信息提取到一个漂亮的表中。但是,我无法将表格写成一个表格,它输出为每个项目的单个表格。

我的输出表格的代码:

f = open("out2.txt", "w") 
for item in table: 
    f.write("%s\n" % item) 
 
f.write("There are %(count)d errors found" %{"count": count}) 

我的输出如下:

我怎样才能把表格写成一个连续的表格?

请您参考如下方法:

f.write(table.get_string()) 适合你吗?不是遍历表格并写入每个项目,而是直接写入表格本身?

像这样,替换循环:

f = open('out2.txt','wb') 
f.write(table.get_string()) 
f.write('There are %(count)d errors found' % {'count': count})