简单Python 文件I/O 拼写检查程序
Simple Python File I/O spell check program
对于 class 我必须创建一个简单的拼写检查程序,它将两个文件作为输入,一个包含正确拼写的单词,另一个包含包含一些拼写错误单词的段落。我以为我已经弄清楚了,但我遇到了一个我以前从未见过的错误。当程序完成时,它会给出错误:
<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()
这不是错误,它完全按照您的指示执行。
这一行:
print(check_words)
你是在告诉它打印一个函数。您看到的输出只是 Python 打印函数的名称及其地址:"printing the function".
是的,不做 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()
对于 class 我必须创建一个简单的拼写检查程序,它将两个文件作为输入,一个包含正确拼写的单词,另一个包含包含一些拼写错误单词的段落。我以为我已经弄清楚了,但我遇到了一个我以前从未见过的错误。当程序完成时,它会给出错误:
<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()
这不是错误,它完全按照您的指示执行。
这一行:
print(check_words)
你是在告诉它打印一个函数。您看到的输出只是 Python 打印函数的名称及其地址:"printing the function".
是的,不做 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()