Yoav
Yoav

Reputation: 501

IPython: quickly dump the output of a cell to file

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.

Upvotes: 9

Views: 5172

Answers (1)

sherdim
sherdim

Reputation: 1236

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

Upvotes: 1

Related Questions