相关疑难解决方法(0)

如何在iPython笔记本中保存单元格的输出?

我希望能够将iPython笔记本单元的TEXT输出保存到磁盘上的文件中.

我有2个额外的要求/要求:

  • 能够重新运行单元格并用最新的内容覆盖我的输出.
  • 还显示笔记本内的输出.

我已经想出如何使用%%capture魔法将iPython笔记本电脑的单元基本保存到一个文件中,但它看起来不够灵活:每次重新运行单元格时它都会不断添加,我无法在其中显示相同的细胞.

这是我到目前为止:

%%capture cap --no-stderr
print 'stuff'
with open('output.txt', 'w') as f:
    f.write(cap.stdout)

# clear the cap by deleting the variable here?
# del cap 
Run Code Online (Sandbox Code Playgroud)

当我尝试cap.show()写入后,它似乎没有显示.相反,它将输出放入cap变量两次.

ipython ipython-notebook ipython-magic

16
推荐指数
1
解决办法
2万
查看次数

IPython:快速将单元格的输出转储到文件

将ipython笔记本单元格或多个单元格保存到文件或pastebin非常容易.

但有时我想将一些操作的输出转储到文件中.这样做的快捷方式是什么?

%save output Out[56]
Run Code Online (Sandbox Code Playgroud)

给我:

Out[56] is neither a string nor a macro.
Run Code Online (Sandbox Code Playgroud)

如果我这样做:

 %save output str(Out[56])
Run Code Online (Sandbox Code Playgroud)

它工作但我失去了漂亮的打印格式,并在输出文件名中添加了自动.py扩展名.

ipython

8
推荐指数
1
解决办法
4737
查看次数

标签 统计

ipython ×2

ipython-magic ×1

ipython-notebook ×1