这就是我所拥有的,评论描述了我想要做什么
在文本文件中放置了一些单词,其中一些单词拼写错误,并且测试文本文件也将用于拼写检查。
例如 >>> spellCheck("test1.txt") {'exercsie': 1, 'finished': 1}
from string import ascii_uppercase, ascii_lowercase
def spellCheck(textFileName):
# Use the open method to open the words file.
# Read the list of words into a list named wordsList
# Close the file
file=open("words.txt","r")
wordsList = file.readlines()
file.close()
# Open the file whos name was provided as the textFileName variable
# Read the text from the file into a list called wordsToCheck
# Close the file
file=open(textFileName, "r")
wordsToCheck = file.readlines()
file.close()
for i in range(0,len(wordsList)): wordsList[i]=wordsList[i].replace("\n","")
for i in range(0,len(wordsToCheck)): wordsToCheck[i]=wordsToCheck[i].replace("\n","")
# The next line creates the dictionary
# This dictionary will have the word that has been spelt wrong as the key and the number of times it has been spelt wrong as the value
spellingErrors = dict(wordsList)
# Loop through the wordsToCheck list
# Change the current word into lower case
# If the current word does not exist in the wordsList then
# Check if the word already exists in the spellingErrors dictionary
# If it does not exist than add it to the dictionary with the initial value of 1.
# If it does exist in the dictionary then increase the value by 1
# Return the dictionary
char_low = ascii_lowercase
char_up = ascii_uppercase
for char in wordsToCheck[0]:
if char in wordsToCheck[0] in char_up:
result.append(char_low)
for i in wordsToCheck[0]:
if wordsToCheck[0] not in wordsList:
if wordsToCheck[0] in dict(wordsList):
dict(wordsList) + 1
elif wordsToCheck[0] not in dict(wordsList):
dict(wordsList) + wordsToCheck[0]
dict(wordsList) + 1
return dict(wordsList)
我的代码返回错误
Traceback(最近一次调用最后):文件“”,第 1 行,在 spellCheck("test1.txt") 文件“J:\python\SpellCheck(1).py”,第 36 行,在 spellCheck spellingErrors = dict(wordsList) ValueError:字典更新序列元素 #0 的长度为 5;2 是必需的
那么任何人都可以帮助我吗?