以干净有序的格式打印新文件的输出

问题描述 投票:0回答:1

我正在尝试使用代码来创建名为output.txt的文件。

但是,当我打印下面的代码然后查看实际的.txt文件时,它会将它全部打印在一行上,这不是我想要的。

 file2.write("*******************************************")
 file2.write("                   ""TICKET REPORT""                   ")
 file2.write("")
 file2.write("There are 6 tickets in the database.")
 file2.write("")
 file2.write("Maximum Ticket price is $179.99.")
 file2.write("Minimum Ticket price is $49.99.")
 file2.write("Average Ticket Price is $149.99.")
 file2.write("")
 file2.write("Thank you for using our ticket system!")
 file2.write("")
 file2.write("*******************************************")
 file2.close()

我希望文本逐行打印,如下所示。请记住,我正在运行Windows 10。

python python-3.7
1个回答
1
投票

只需在行尾添加'\n'或其他解决方案是:

lines = [line0, line1, .... ]
fo = open("foo.txt", "rw+")
fo.writelines(lines)
© www.soinside.com 2019 - 2024. All rights reserved.