Python 有人能帮我查一下拼写代码吗?

Python 有人能帮我查一下拼写代码吗?,python,python-2.7,Python,Python 2.7,这就是我的评论,描述了我想做的事情 有些单词放在一个文本文件中,其中一些单词拼写错误,还有测试文本文件,这些文件将用于拼写检查 e、 g.>>>拼写检查测试1.txt {'exercsie':1,'finished':1} from string import ascii_uppercase, ascii_lowercase def spellCheck(textFileName): # Use the open method to open the words file. #

这就是我的评论,描述了我想做的事情

有些单词放在一个文本文件中,其中一些单词拼写错误,还有测试文本文件,这些文件将用于拼写检查

e、 g.>>>拼写检查测试1.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)
我的代码返回一个错误

回溯最近一次呼叫上次: 文件,第1行,在 拼写检查测试1.txt 文件J:\python\SpellCheck1.py,第36行,拼写检查 拼写错误=听写单词列表 ValueError:字典更新序列元素0的长度为5;2是必需的

有人能帮我吗?

我应用并重写了非音速代码

import collections

def spell_check(text_file_name):
    # dictionary for word counting
    spelling_errors = collections.defaultdict(int)

    # put all possible words in a set
    with open("words.txt") as words_file:
        word_pool = {word.strip().lower() for word in words_file}

    # check words
    with open(text_file_name) as text_file:
        for word in (word.strip().lower() for word in text_file):
            if not word in word_pool:
                spelling_errors[word] += 1

    return spelling_errors
您可能需要阅读有关和的信息

使用ascii_大写字母和ascii_小写字母编写代码:并学习基础知识。这些代码是我不知道我在做什么,但我还是做了

有关旧代码的更多说明:

你用

char_low = ascii_lowercase
不需要char_low,因为您从不操纵该值。只需使用原始ascii_小写字母即可。然后是代码的以下部分:

for char in wordsToCheck[0]:
    if char in wordsToCheck[0] in char_up:
        result.append(char_low)
我不太清楚你想在这里做什么。似乎您希望将列表中的单词转换为小写。事实上,如果该代码能够运行(它没有运行),您将把整个小写字母表附加到列表中单词的每个大写字符的resultf中。不过,在后面的代码中不使用result,因此不会造成任何伤害。在循环之前添加一个打印字来检查[0]或在循环中添加一个打印字符来查看那里发生了什么是很容易的

代码的最后一部分就是一团糟。您只访问每个列表中的第一个单词-可能是因为您不知道该列表是什么样子。这就是通过尝试和错误编码。试着用知识来编码

你真的不知道dict是做什么的,也不知道如何使用它。我可以在这里解释,但在www.python.org上有一篇精彩的教程,你可能想先读一读,尤其是。如果您研究了这些解释,但仍然不理解,请随时提出与此相关的新问题

我使用defaultdict而不是标准字典,因为它使这里的生活更轻松。如果您将拼写错误定义为dict,则我的代码的一部分必须更改为

if not word in word_pool:
    if not word in spelling_errors:
        spelling_errors[word] = 1
    else:
        spelling_errors[word] += 1

顺便说一句,我写的代码为我运行没有任何问题。我得到一本字典,其中缺少的单词小写作为键,该单词的计数作为相应的值。

真的吗?返回一个空的dict?此外,dict+1会引发typeerror当然它会返回一个空dict:dict就是这样!您的问题与如何实现拼写检查器无关,而与基本Python有关。也许可以修改你的问题来澄清实际的问题是什么。你的代码没有返回我想要的,它应该返回拼写错误的单词。你是对的。我不小心用了字符串“word”而不是变量词。我修正了。你能编辑我的代码以得到正确的输出吗?我想看看我哪里出了问题。我对python和代码都很陌生,需要考虑一些事情:为什么你认为你需要低字符和高字符?wordsToCheck[0]的内容是什么?什么是单词列表?你认为dictwordsList+1会发生什么?不要猜测,用打印机看看到底发生了什么。现在欧洲已经过了午夜。上床睡觉。我让char_low和char_up将单词的字母改为小写,而wordstocheck[0]我希望这将应用于wordstocheck列表中的第一个索引。dictwordslist我也不是很确定我不确定如何实现dict函数,我只是尝试一下,我无法使用print查看代码的结尾部分,因为我遇到了一个错误,因为我不知道如何实现dict函数