如果有警告,如何停止程序

gal*_*yan 6 python

我尝试使用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():*
Run Code Online (Sandbox Code Playgroud)

Dav*_*dmh 9

警告是非致命错误.有些事情是错的,但程序可以继续.

它们可以使用标准库模块warnings或通过命令行进行处理,并传递标志-Werror.编程方式:

import warnings

with warnings.catch_warnings():
    warnings.simplefilter('error')
    function_raising_warning()
Run Code Online (Sandbox Code Playgroud)