[英]Saving output in txt file
I have a python program that gives 3000 outputs in five seconds, and it gives millions of outputs in minutes.我有一个 python 程序,它可以在 5 秒内提供 3000 个输出,并在几分钟内提供数百万个输出。 So I wanna know how to save the outputs in a '.txt' file automatically.
所以我想知道如何将输出自动保存在“.txt”文件中。
I think I need a python program to save all my outputs in a.txt file.我想我需要一个 python 程序来将我的所有输出保存在一个 .txt 文件中。 I use python 2 for some reason.
出于某种原因,我使用 python 2 。
The problem I have is,我遇到的问题是
Help me guys, Thanks in advance.帮帮我,提前谢谢。
By output I am assuming you are talking about print()
statements.通过 output 我假设您正在谈论
print()
语句。 You can redirect your standard output to have print()
go to a file instead:您可以将标准 output 重定向到
print()
go 到文件:
import sys
with open('output.txt','w') as f:
sys.stdout = f
print('Hello')
Add this piece of code before you start outputting:在开始输出之前添加这段代码:
f = open(“file.txt”, “w+”)
Add this piece of code during outputting:在输出过程中添加这段代码:
f.write(outputvar)
Add this piece of code after outputting:输出后添加这段代码:
f.close()
I hope this helps, if it doesn't, please tell me.我希望这会有所帮助,如果没有,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.