simplysoso
simplysoso

Reputation: 13

Using dictionaries in python

I have to define a function names Correct(), which has two parameters. A list of strings to be tested for misspelled words(this parameter will be my file), and my dictionary, which I have already made called mydictionary. The function should test each word in the first list using the dictionary and the check word() function. correct should return a list of words with all the misspelled words replaced. For example, correct([‘the’, ‘cheif’, ‘stopped’, ‘the’, ‘theif’]) should return the list [‘‘the’, ‘chief’, ‘stopped’, ‘the’, ‘thief’] Then I'm supposed to test the function in the main()

import string
# Makes a function to read a file, use empty {} to create an empty dict
# then reads the file by using a for loop
def make_dict():
    file = open ("spellingWords.txt")
    dictionary = {}
    for line in file:
        # Splits the lines in the file as the keys and values, and assigning them as
# misspell and spell
        misspell, spell = string.split(line.strip())
        # Assigns the key to the value
        dictionary[misspell] = spell

    file.close()


    return dictionary

mydictionary = make_dict()



#print mydictionary

# Gets an input from the user
word = raw_input("Enter word")
# Uses the dictionary and the input as the parameters
def check_word(word,mydictionary):
    # Uses an if statement to check to see if the misspelled word is in the
    # dictionary
    if word in mydictionary:
        return mydictionary[word]
    # If it is not in the dictionary then it will return the word
    else:
        return word

# Prints the function
print check_word(word,mydictionary)

def main():
    file2 = open ("paragraph.txt")
    file2.read()
    thelist = string.split(file2)
    print thelist
    def correct(file2,mydictionary):
        return thelist
        paragraph = string.join(thelist)
        print paragraph

main()

All my other functions work besides my Correct() function and my Main() function. It is also giving me the error 'file object has no attribute split'. Can I get help with my mistakes?

so I corrected it and now have for my main function

def main():
    file2 = open ("paragraph.txt")
    lines = file2.read()
    thelist = string.split(lines)
    def correct(file2,mydictionary):
        while thelist in mydictionary:
            return mydictionary[thelist]
    paragraph = string.join(thelist)
    print correct(file2,mydictionary)

however I am now getting the error 'unhashable type: 'list''

Upvotes: 0

Views: 95

Answers (1)

Zizouz212
Zizouz212

Reputation: 4998

You are not understanding the concept of reading file object. You have to store the value that the method read() returns so that you can use it later.

open simply returns a file object, not the lines of a file. To read spy tuff, you can simply use methods to do things. See the docs

To read the file:

lines = file.read()
# do something with lines - it's a super big string

Also, you don't have to import string, the split() method is already built in with strings.

Upvotes: 1

Related Questions