How to write the output of ipython command in pyth

2019-03-10 23:26发布

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.

标签: ipython
4条回答
淡お忘
2楼-- · 2019-03-11 00:00

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
查看更多
混吃等死
3楼-- · 2019-03-11 00:07

Use the IPython magic function store

%store foo >> a.txt # Append value of foo to file a.txt
查看更多
不美不萌又怎样
4楼-- · 2019-03-11 00:15

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.

查看更多
一纸荒年 Trace。
5楼-- · 2019-03-11 00:16

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?
查看更多
登录 后发表回答