将新行输出到 txt 文件。 (Python 3.2)
全部。我在将新行输出到 txt 文件时遇到问题。输出很好,但全部都在一行中。关于如何解决的任何想法?
#Double numberer
end=100
count=0
count=int(input("What number would you like to start?"))
end=int(input("What number would you like to end?"))
biglist = []
logfile=open("output.txt", mode="w", encoding="utf-8")
while count < end+1:
logfile.write(str(count),"\n")
logfile.write(str(count),"\n")
print(count)
count += 1
print("done")
logfile.close()
all. I'm having trouble outputting new lines to a txt file. The output is good but it's all in one line. Any ideas on how to work around?
#Double numberer
end=100
count=0
count=int(input("What number would you like to start?"))
end=int(input("What number would you like to end?"))
biglist = []
logfile=open("output.txt", mode="w", encoding="utf-8")
while count < end+1:
logfile.write(str(count),"\n")
logfile.write(str(count),"\n")
print(count)
count += 1
print("done")
logfile.close()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要将新行连接到字符串,请使用
+
符号:另外,请注意,您的
while
循环每次递增count
是不必要的Python。你可以这样做:To concatenate the new lines to the string, use a
+
symbol:Also, be aware that your
while
loop incrementingcount
each time is unnecessary in Python. You could just do this:一种简单的方法是:
One simple way is: