如何抑制stdout
输出?
例如,分号可用于抑制返回对象的显示
>>> 1+1
2
>>> 1+1; # No output!
但是,打印到stdout的函数不受分号影响。
>>> print('Hello!')
Hello!
>>> MyFunction()
Calculating values...
如何才能从输出让print
/ MyFunction
被抑制?
发布于 2019-03-21 13:11:38
添加%%capture
为单元格的第一行。例如
%%capture
print('Hello')
MyFunction()
这只是丢弃输出,但%%capture
可以使用魔法将输出保存到变量 - 请参阅文档
发布于 2019-03-21 14:30:16
(https://stackoverflow.com/a/23611571/389812)
你可以使用io.capture_output:
from IPython.utils import io
with io.capture_output() as captured:
MyFunction()
抑制(例如捕获)stdout和stderr中的那些行with-statement
。
https://stackoverflow.com/questions/-100006484
复制相似问题