Python:如何将代码的输出导出到自己的文本文件中?

Jet*_*rey 1 python ipython python-2.7 python-3.x

如何将代码的输出导出到自己的文本文件中?当我运行我的代码时,我从中获取了大量数据.如何导出它以便我可以读取其自己的文本文件中的所有数据行.

del*_*lta 5

你可以用python写文件

with open("out.txt", "w") as f:
    f.write("OUTPUT")
Run Code Online (Sandbox Code Playgroud)

或者您可以使用io重定向将输出重定向到文件

$ python code.py > out.txt
Run Code Online (Sandbox Code Playgroud)
  1. https://docs.python.org/3/tutorial/inputoutput.html#methods-of-file-objects
  2. https://en.wikipedia.org/wiki/Redirection_(computing)