简体   繁体   English

如何将iPython Notebook的全部输出另存为.txt文件?

[英]How can I save my entire output from iPython notebook as .txt file?

I have written a program to crawl data from twitter in ipython notebook. 我已经编写了一个程序来在ipython Notebook中从Twitter爬网数据。 The program gives enormous streams of data as output and I want to save this output in .txt file. 该程序将大量数据流作为输出,我想将此输出保存在.txt文件中。 How do I do it? 我该怎么做? When I open my terminal,I can easily do it by: python myfile.py>file.txt How do I do the same thing in ipython notebook? 当我打开终端时,我可以通过以下方式轻松地做到这一点:python myfile.py> file.txt如何在ipython Notebook中做同样的事情?

I think below code sniplet will help you. 我认为下面的代码片段将为您提供帮助。 I am simply changing stdout to point to some file. 我只是在更改stdout以指向某个文件。 Whatever the output be after that will get written into that file. 之后的任何输出将被写入该文件。

Later I am changing the stdout back to its original form. 稍后,我将标准输出变回其原始形式。

import sys

# Holding the original output object. i.e. console out
orig_stdout = sys.stdout

# Opening the file to write file deletion logs.
f = open('file.txt', 'a+')

# Changing standard out to file out. 
sys.stdout = f

# Any print call in this function will get written into the file.
myFunc(params)
# This will write to the file. 
print("xyz") 

# Closing the file.
f.close()

# replacing the original output format to stdout.
sys.stdout = orig_stdout

# This will print onto the console.
print("xyz") 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将我的 python 输出从 XML 文件保存到 txt - how can I save my python output from XML file to txt 如何保持 IPython 笔记本中显示的 output ? - How can I keep output shown in the IPython notebook? Matplotlib:将图形另存为 iPython 笔记本中的文件 - Matplotlib: Save figure as file from iPython notebook 如何将终端 output 从某个文件夹(ls 命令)保存到 txt 文件? 对于 Pycharm - How can I save terminale output from certain folder (ls command) to txt file? For Pycharm 有没有一种方法可以将整个jupyter笔记本的输出保存到txt文件或类似文件中,而无需使用命令行,而是从笔记本内部进行保存? - Is there a way to save the output of a whole jupyter notebook to a txt file or similar without using command line but from inside the notebook? 如何使用python写入输出的txt文件? - How can I write to a txt file of my output using python? 我可以从笔记本重启 iPython 集群吗? - Can I restart an iPython cluster from a notebook? 如何将 Python Tkinter 的输入保存到 txt 文件中? - How can I save inputs from Python Tkinter into a txt file? 如何将所有 ipython 输出保存在文件中? (也许像 html?) - How can I save ALL my ipython outputs in a file? (maybe like html?) 如何将 IPython 控制台的输出保存到 Spyder 中的文件? - How to save the output of an IPython console to a file in Spyder?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM