Python社区  »  Python

简单的Python文件I/O拼写检查程序

liam12808 • 2 周前 • 43 次点击  

<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社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/56510
 
43 次点击  
分享到微博
文章 [ 2 ]  |  最新文章 2 周前
gerwin
Reply   •   1 楼
gerwin    11 月前

这不是一个错误,它完全按照你说的去做。

这一行:

print(check_words)

你要它打印一个函数。您看到的输出只是Python打印函数名和地址:“打印函数”。

MarsNebulaSoup
Reply   •   2 楼
MarsNebulaSoup    11 月前

是的,别这样 print(check_words) ,做 print(check_words())

check_words(dpath, fpath) misspelled_words = check_words(dpath, fpath)

和改变 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()