我正在尝试使用代码来创建一个名为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。
答案 0 :(得分:1)
只需在行或的末尾添加'\n'
,或者其他解决方案是:
lines = [line0, line1, .... ]
fo = open("foo.txt", "rw+")
fo.writelines(lines)