IPython: quickly dump the output of a cell to file

拜拜、爱过 提交于 2019-12-08 17:11:34

问题


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]

gives me:

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

And if I do:

 %save output str(Out[56])

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


回答1:


to keep pretty-print formatting try

%save output repr(_56)

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

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


来源:https://stackoverflow.com/questions/17686684/ipython-quickly-dump-the-output-of-a-cell-to-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!