【问题标题】:Simple Python File I/O spell check program简单的 Python 文件 I/O 拼写检查程序
【发布时间】:2019-05-04 22:01:31
【问题描述】:

对于一个班级,我必须创建一个简单的拼写检查程序,该程序接受两个文件作为输入,一个包含正确拼写的单词,一个包含一个包含一些拼写错误单词的段落。我以为我已经弄清楚了,但我遇到了一个我以前从未见过的错误。当程序完成时,它会给出错误:

<function check_words at 0x7f99ba6c60d0>

我从来没有见过这个,也不知道它是什么意思,如果能帮助这个程序运行,我将不胜感激。程序代码如下:

import os
def main():
    while True:
        dpath = input("Please enter the path to your dictionary:")
        fpath = input("Please enter the path to the file to spell check:")
        d = os.path.isfile(dpath)
        f = os.path.isfile(fpath)

        if d == True and f == True:
            check_words(dpath, fpath)
            break

    print("The following words were misspelled:")
    print(check_words)

def linecheck(word, dlist):
    if word in dlist:
        return None
    else:
        return word

def check_words(dictionary, file_to_check):
    d = dictionary
    f = file_to_check
    dlist = {}  
    wrong = []  


    with open(d, 'r') as c:
        for line in c:
            (key) = line.strip()
            dlist[key] = ''

    with open(f, 'r') as i:
        for line in i:
            line = line.strip()
            fun = linecheck(line, dlist)
            if fun is not None:
                wrong.append(fun)

    return wrong

if __name__ == '__main__':
    main()

【问题讨论】:

  • 调用函数来执行它

标签: python-3.x string file-io text-files spell-checking


【解决方案1】:

这不是错误,它完全按照您的指示行事。

这一行:

print(check_words)

你告诉它打印一个函数。您看到的输出只是 Python 打印函数的名称及其地址:“打印函数”。

【讨论】:

    【解决方案2】:

    是的,不要做print(check_words),做print(check_words())

    此外,将check_words(dpath, fpath) 更改为misspelled_words = check_words(dpath, fpath)

    并将print(check_words) 更改为print(misspelled_words)

    最终代码(稍作修改):

    import os
    def main():
        while True:
            dpath = input("Please enter the path to your dictionary: ")
            fpath = input("Please enter the path to the file to spell check: ")
            d = os.path.isfile(dpath)
            f = os.path.isfile(fpath)
    
            if d == True and f == True:
                misspelled_words = check_words(dpath, fpath)
                break
    
        print("\nThe following words were misspelled:\n----------")
        #print(misspelled_words) #comment out this line if you are using the code below
    
        #optional, if you want a better looking output
    
        for word in misspelled_words:   # erase these lines if you don't want to use them
            print(word)                 # erase these lines if you don't want to use them
    
        #------------------------ 
    
    
    def linecheck(word, dlist):
        if word in dlist:
            return None
        else:
            return word
    
    def check_words(dictionary, file_to_check):
        d = dictionary
        f = file_to_check
        dlist = {}  
        wrong = []  
    
    
        with open(d, 'r') as c:
            for line in c:
                (key) = line.strip()
                dlist[key] = ''
    
        with open(f, 'r') as i:
            for line in i:
                line = line.strip()
                fun = linecheck(line, dlist)
                if fun is not None:
                    wrong.append(fun)
    
        return wrong
    
    if __name__ == '__main__':
        main()
    

    【讨论】:

    • 修复了所有我认为我遇到的问题,但又遇到了新问题。现在程序说任何大写的单词都拼错了。我对 Python 很陌生,所以我可以调用任何类型的 .equalsIgnoreCase 函数让它忽略大写吗?
    • 你可以将单词转换为小写然后检查它。像这样:word.lower()
    • 我可以把它放在 linecheck 函数中,比如 "if word.lower() in dlist:" 吗?
    • 那么,linecheck() 是否真的会检查单词是否拼写错误?
    • 不,它只是将该行中的单词与 dlist 进行比较,所以我想把它放在哪里
    猜你喜欢
    • 2014-10-28
    • 1970-01-01
    • 1970-01-01
    • 2018-06-29
    • 2011-09-20
    • 2012-07-20
    • 2017-02-27
    • 1970-01-01
    相关资源
    最近更新 更多