如何在出现警告时停止程序
我在用nltk做一些文字处理的时候,遇到了一个警告。我发现如果有像"Nations�"这样的词,程序就会发出警告。我想知道有没有办法在出现警告后让程序停止运行。谢谢。
警告信息:
*UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
if word[0].lower() not in stopwords.words():*
2 个回答
3
python -Werror ...
(在被接受的答案中出现过,但稍微有点隐蔽)
15
警告是一种非致命的错误。这意味着程序出现了一些问题,但仍然可以继续运行。
你可以使用标准库模块 warnings
来处理这些警告,或者通过命令行传递 -Werror
这个标志来处理。程序化地处理警告的方法如下:
import warnings
with warnings.catch_warnings():
warnings.simplefilter('error')
function_raising_warning()