38

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.

0

4 Answers 4

51

Use the IPython magic function store

%store foo >a.txt # Store (overwrite) value of foo to file a.txt

%store foo >>a.txt # Append value of foo to file a.txt
Sign up to request clarification or add additional context in comments.

4 Comments

Is there a way to read it back to a variable in a similar way? Say, I stored a list to a .txt, and I want to read the .txt back to a variable. Thank you.
you can try with %load. Although the best practice is to store the file with pickle.
Beware that with %store, data type like str are saved as (<class 'str'>,), and when %load it raises an error... Definitely, go with pickle!
beware you have to type it like this: %store foo >a.txt to work. If you type it in this way: %store foo > a.txt you will get a FileNotFound error.
30

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

Comments

8

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.

1 Comment

Yep. You should use x=my_print_output.stdout %store x > file
1

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?

1 Comment

Hmm, when I try this on a numpy array I get 'NoneType' object is not iterable

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.