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.
clear_output to clear the output of a cell.
Capturing Output With %%capture IPython has a cell magic, %%capture , which captures the stdout/stderr of a cell. With this magic you can discard these streams or store them in a variable. By default, %%capture discards these streams. This is a simple way to suppress unwanted output.
Both ! and % allow you to run shell commands from a Jupyter notebook. % is provided by the IPython kernel and allows you to run "magic commands", many of which include well-known shell commands. ! , provided by Jupyter, allows shell commands to be run within cells.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With