如何阻止 python RuntimeWarning 打印到终端?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27864001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I block python RuntimeWarning from printing to the terminal?
提问by Kieran Hunt
I have some python code that, at some point, takes the axis means of a reasonably sparse array with nans in. Every time I run the code it raises a RuntimeWarning.
我有一些 python 代码,在某些时候,它采用带有 nans 的合理稀疏数组的轴平均值。每次我运行代码时,它都会引发一个RuntimeWarning.
I know what's causing the warning, it's intentional, and it doesn't affect the output. It is, however, quite irritating to be presented with the warning every time I run the program - so, is there a cheap and nasty way to prevent them from being printed to the terminal?
我知道是什么导致了警告,这是故意的,并且不会影响输出。然而,每次我运行程序时都会出现警告是非常令人恼火的 - 那么,是否有一种廉价而讨厌的方法来防止它们被打印到终端?
采纳答案by Palmer
This may be useful to you, I think the issue has been pretty solved in this question: How to disable python warnings
这可能对你有用,我认为这个问题已经很好地解决了这个问题:How to disable python warnings
回答by ZedXter
All what you need is suppress it exactly as described in official documentation: https://docs.python.org/2/library/warnings.html#temporarily-suppressing-warnings
您需要做的就是完全按照官方文档中的描述抑制它:https: //docs.python.org/2/library/warnings.html#temporously-suppressing-warnings

