我希望将以下ipython命令的输出捕获到一个文件中:命令和输出区域如下:
`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)
我只想将“欢迎加入我的谈话”这一部分捕获到我的文件中。
发布于 2015-09-23 13:10:39
使用IPython神奇函数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
发布于 2013-01-16 13:07:24
只需执行以下操作:
%save file_name.py _oh[7]
PS:其他一些有用的命令:
%save file_name.py _
'_‘指的是之前的输出。
或者,您可以:
%save file_name.py _oh[i]
'i‘为输出历史编号,可通过以下方式首先查看输出:
_oh
发布于 2018-03-26 20:31:27
如果需要的话,%%capture
cell magic会保存运行命令的stdout/stderr输出。下面是用法语法:
%%capture [--no-stderr] [--no-stdout] [--no-display] [output]
下面是一个使用示例:
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
输出对象是IPython.utils.capture.CapturedIO
的一个实例,它有一个简洁的接口,用于访问标准输出/标准错误或组合输出。
https://stackoverflow.com/questions/13199170
复制相似问题