I want to capture the output of the following ipython command into a file:
commands and outputs areas follows:
`decoder.get_hyp()`
WARNING: "ngram_search.c", line 1000: </s> not found in last frame, using ++NOISE++ instead
INFO: ngram_search.c(1046): lattice start node <s>.0 end node ++NOISE++.171
INFO: ps_lattice.c(1225): Normalizer P(O) = alpha(++NOISE++:171:185) = -2003082
INFO: ps_lattice.c(1263): Joint P(O,S) = -2036704 P(S|O) = -33622
Out[7]: ('WELCOME TO MY TALK', '000000000', -36704586)
I want to capture only the part "wellcome to my talk" into my file.
Use the IPython magic function store
%store foo >> a.txt # Append value of foo to file a.txt
Just do as follow:
%save file_name.py _oh[7]
PS: Some additional useful command:
%save file_name.py _
'_' refers to the previous output.
Or you can:
%save file_name.py _oh[i]
'i' refers to the output history number, you can see output first via:
_oh
IPython captures the value (output) of the last command in the variable _ (underscore).
%edit some_variable
will open the value of a variable in your editor.
So, "%edit _", should enable you to edit and save the value of the last command.
See the History section of the IPython docs
And to learn about the possible arguments to the %edit magic function, type the following at the ipython prompt:
%edit?
The %%capture
cell magic saves the stdout/stderr output of running a command, if that's what you need. Here's the usage syntax:
%%capture [--no-stderr] [--no-stdout] [--no-display] [output]
And, here's a usage example:
In [1]: %%capture my_print_output
...: print('test')
...:
In [2]: my_print_output
Out[2]: <IPython.utils.capture.CapturedIO at 0x7f2efa2c12d0>
In [3]: test_output.show()
test
The output object is an instance of IPython.utils.capture.CapturedIO
, which has a neat interface for accessing stdout/stderr or combined output.