简体   繁体   English

Python如何摆脱PyDictionary错误消息

[英]Python how to get rid of PyDictionary error messages

I have the following code to check if a word is in the dictionary. 我有以下代码来检查字词中是否有单词。 If the word does not exist the call to dictionary.meaning returns None. 如果该单词不存在,则对dictionary.meaning的调用将返回None。 The problem is that it also spits out an error message "Error: The Following Error occured: list index out of range". 问题是它还会发出错误消息“错误:发生以下错误:列表索引超出范围”。 I did some research and it appeared that I could use a combination of try:, except: but no matter what I tried the error message is still printed out. 我做了一些研究,似乎我可以使用try:的组合,除了:但无论我尝试了什么,仍然打印出错误信息。 Here is a test case that shows the problem. 这是一个显示问题的测试用例。 How can I make this code work without displaying the index error? 如何在不显示索引错误的情况下使此代码正常工作?

Code: 码:

    def is_word(word):
        from PyDictionary import PyDictionary
        dictionary=PyDictionary()
        rtn = (dictionary.meaning(word))
        if rtn == None:
           return(False)
        else:
           return (True)

    my_list = ["no", "act", "amp", "xibber", "xyz"]

    for word in my_list:
        result = is_word(word)
        if result == True:       
           print(word, "is in the dictionary")
        else:
           print(word, "is NOT in the dictionary")

Output: 输出:

no is in the dictionary
act is in the dictionary
amp is in the dictionary
Error: The Following Error occured: list index out of range
xibber is NOT in the dictionary
Error: The Following Error occured: list index out of range
xyz is NOT in the dictionary

I'm guessing your try/except block was around the wrong block, or you weren't catching it properly, but it's tough to tell without your code. 我猜你的try / except块是在错误的块附近,或者你没有正确捕获它,但是没有你的代码很难说。

Try putting the try/except around the section of code that would be erroring (the dictionary check in this case). 尝试将try / except放在可能出错的代码部分周围(在本例中为字典检查)。

EDIT: 编辑:

My mistake. 我的错。 The error is getting printed by the PyDictionary library . 错误由PyDictionary打印。 You should be able to silence it by doing meaning(word, disable_errors=True) . 你应该能够通过做出meaning(word, disable_errors=True)来使它沉默meaning(word, disable_errors=True)

def is_word(word):
    from PyDictionary import PyDictionary

    dictionary = PyDictionary()

    try:
        output = dictionary.meaning(word, disable_errors=True)
    except:
        return False
    else:
        return bool(output)

my_list = ["no", "act", "amp", "xibber", "xyz"]

for word in my_list:
    result = is_word(word)
    if result:       
       print("{} is in the dictionary".format(word))
    else:
       print("{} is NOT in the dictionary".format(word))

Second Edit: Using https://github.com/tasdikrahman/vocabulary . 第二次编辑:使用https://github.com/tasdikrahman/vocabulary

from vocabulary.vocabulary import Vocabulary
vb = Vocabulary()

my_list = ["no", "act", "amp", "xibber", "xyz"]

for word in my_list:
    if vb.meaning(word):
       print("{} is in the dictionary".format(word))
    else:
       print("{} is NOT in the dictionary".format(word))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM