执行一个python脚本(这里包括很长的方式)我写了一个警告消息.我不知道我的代码中的哪一行会被引发.我怎样才能获得这些信息?
此外,这究竟意味着什么?事实上,我不知道我使用的是某种蒙面数组?
/usr/lib/pymodules/python2.7/numpy/ma/core.py:3785: UserWarning: Warning: converting a masked element to nan.
warnings.warn("Warning: converting a masked element to nan.")
Run Code Online (Sandbox Code Playgroud)
War*_*ser 13
您可以使用该warnings模块将警告转换为异常.调用最简单的方法simplefilter.这是一个例子; 生成警告的代码在func2b()中,因此存在非特异性回溯.
import warnings
def func1():
print "func1"
def func2():
func2b()
print "func2"
def func2b():
warnings.warn("uh oh")
def func3():
print "func3"
if __name__ == "__main__":
# Comment the following line to see the default behavior.
warnings.simplefilter('error', UserWarning)
func1()
func2()
func3()
Run Code Online (Sandbox Code Playgroud)
当包含调用的行simplefilter被注释掉时,输出为
func1
warning_to_exception.py:13: UserWarning: uh oh
warnings.warn("uh oh")
func2
func3
Run Code Online (Sandbox Code Playgroud)
包含该行后,您将获得追溯:
func1
Traceback (most recent call last):
File "warning_to_exception.py", line 23, in <module>
func2()
File "warning_to_exception.py", line 9, in func2
func2b()
File "warning_to_exception.py", line 13, in func2b
warnings.warn("uh oh")
UserWarning: uh oh
Run Code Online (Sandbox Code Playgroud)