Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python文本文件中写入ipython命令的输出?_Ipython - Fatal编程技术网

如何在python文本文件中写入ipython命令的输出?

如何在python文本文件中写入ipython命令的输出?,ipython,Ipython,我想将以下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_la

我想将以下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)
`decoder.get_hyp()`
警告:“ngram_search.c”,第1000行:未在最后一帧中找到,请改用++NOISE++
信息:ngram_search.c(1046):晶格开始节点.0结束节点++噪声+++.171
信息:ps_lattice.c(1225):归一化器P(O)=α(++噪声++:171:185)=-2003082
信息:ps|u lattice.c(1263):关节P(O,S)=-2036704 P(S|O)=-33622
Out[7]:(“欢迎来到我的演讲”,“000000000”,-36704586)

我只想将“wellcome to my talk”部分捕获到我的文件中。

IPython捕获变量uz(下划线)中最后一个命令的值(输出)

将在编辑器中打开变量的值

因此,“%edit"”应允许您编辑并保存最后一个命令的值

要了解%edit magic函数的可能参数,请在ipython提示符下键入以下内容:

%edit?
只需按照以下步骤操作:

%save file_name.py _oh[7]
PS:一些额外的有用命令:

%save file_name.py _
“\”是指以前的输出

或者你可以:

%save file_name.py _oh[i]
“i”是指输出历史编号,您可以通过以下方式首先查看输出:

_oh

使用IPython魔术功能

如果您需要,则保存运行命令的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
[1]中的
:%%捕获我的打印输出
…:打印('测试')
...:
在[2]:我的打印输出
出[2]:
在[3]:test_output.show()中
测试

输出对象是的一个实例,它有一个整洁的接口用于访问stdout/stderr或组合输出。

Hmm,当我在numpy数组上尝试此操作时,我得到的
“NoneType”对象不适用
向下投票用于糟糕的问题。编辑用于清理问题/回答。我认为编辑比向下投票和评论花费的时间要少。有没有类似的方法将其读回变量?比如说,我将一个列表存储到一个.txt中,我想把这个.txt读回一个变量。谢谢。您可以尝试使用%load。虽然最佳做法是使用pickle存储文件。但请注意,使用
%store
,类似
str
的数据类型将另存为(,),并且当
%load
时会引发错误。。。当然,来泡菜吧!是的。您应该使用
x=my\u print\u output.stdout
%store x>file
%%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