[英]How do you suppress output in Jupyter running IPython?
How can output to stdout
be suppressed?如何抑制到stdout
的 output?
A semi-colon can be used to supress display of returned objects, for example分号可用于禁止显示返回的对象,例如
>>> 1+1
2
>>> 1+1; # No output!
However, a function that prints to stdout is not affected by the semi-colon.但是,打印到标准输出的 function 不受分号的影响。
>>> print('Hello!')
Hello!
>>> MyFunction()
Calculating values...
How can the output from print
/ MyFunction
be suppressed?如何抑制来自print
/ MyFunction
的 output?
Add %%capture
as the first line of the cell.添加%%capture
作为单元格的第一行。 eg例如
%%capture
print('Hello')
MyFunction()
This simply discards the output, but the %%capture
magic can be used to save the output to a variable - consult the docs这只是丢弃输出,但%%capture
魔法可用于将输出保存到变量 - 请参阅文档
;
放一个;
at the end of a line to suppress the printing of output [ Reference ].在一行的末尾抑制输出的打印 [参考]。A good practice is to always return values from functions rather than printing values inside a function.一个好的做法是始终从函数返回值,而不是在函数内部打印值。 In that case, you have the control;在这种情况下,您拥有控制权; if you want to print the returned value, you can;如果你想打印返回值,你可以; otherwise, it will not be printed just by adding a ;否则,不会仅通过添加 ; 来打印它。 after the function call.在函数调用之后。
(credit: https://stackoverflow.com/a/23611571/389812 ) (信用: https : //stackoverflow.com/a/23611571/389812 )
You could use io.capture_output :您可以使用io.capture_output :
from IPython.utils import io
with io.capture_output() as captured:
MyFunction()
to supress (eg capture) stdout and stderr for those lines within the with-statement
.抑制(例如捕获) with-statement
那些行的 stdout 和 stderr 。
If anyone is interested in clearing all outputs:如果有人有兴趣清除所有输出:
Then choose whichever option you like.然后选择您喜欢的任何选项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.