这个问题在这里已经有了答案:
Saving the output of a cell in jupyter notebook with a new line after each number
(1 个回答)
2年前关闭。
在基于 ipython 的 jupyter 笔记本中,我有一个打印内容的功能。我无法更改此功能,因为它来自库。让我们来
def print_something():
print("something")
举个例子。当我跑
print_something()
在一个单元格中,我想将单元格打印的任何内容保存在一个文件中。我怎么做?我试过 %save
和 %logstart
, 但作为 print
不是创建输出,他们不做我想做的。
最佳答案
感谢评论,我在另一个 Question 中找到了答案:
%%capture cap --no-stderr
print_something()
with open('/path/to/capture.txt') as f:
f.write(cap.stdout)
文档:https://ipython.readthedocs.io/en/stable/interactive/magics.html#cellmagic-capture
关于python - 如何捕获基于 ipython 的 jupyter notebook 中打印的所有内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57568861/