IPython:快速将单元格的输出转储到文件中 [英] IPython: quickly dump the output of a cell to file

查看:107
本文介绍了IPython:快速将单元格的输出转储到文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将ipython笔记本单元格或多个单元格保存到文件或者粘贴框中非常容易。

It is very easy to save an ipython notebook cell or several cells to a file or to pastebin.

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

But sometimes I want to dump the output of some operation to file. What's the fast way of doing that?

%save output Out[56]

给了我:

Out[56] is neither a string nor a macro.

如果我这样做:

 %save output str(Out[56])

它有效但是我丢失了漂亮的打印格式,并在输出文件名中添加了自动.py扩展名。

It works but I lose the pretty-print formatting, and have an automatic .py extension added to the output file name.

推荐答案

保持漂亮-print格式化尝试

to keep pretty-print formatting try

%save output repr(_56)

如果您返回的对象提供 __ str __ __ repr __ 方法它可能有帮助

if an object you return provides __str__ and __repr__ methods it might help

扩展.py或.ipy在IPython魔法核心中是硬编码的。所以每次重命名都比较容易。
或者如果您不打算更新IPython,请更改
... \IPython \ core\magics \ code.py,第188行:

extention .py or .ipy is hardcoded in the IPython magics core. So it is easier to rename each time. or if you are not planning to update IPython, change in ...\IPython\core\magics\code.py , line 188:

    if not fname.endswith((u'.py',u'.ipy')):
        pass #fname += ext

这篇关于IPython:快速将单元格的输出转储到文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆