Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 07-24-2005, 02:29 PM   #1
LQ Newbie
Registered: Jan 2004
Location: Ohio
Distribution: Gentoo
Posts: 26

Rep: Reputation: 15
Problem loading file of words in python

I've been spending today learning python and as an exercise I've ported a program I wrote in java that unscrambles a word. Before describing the problem, here's the code:
# Filename:

def sort_string(word):
        '''Returns word in lowercase sorted alphabetically'''
        word = str.lower(word)
        word_list = []
        for char in word:
        sorted_word = ''
        for char in word_list:
                sorted_word += char
        return sorted_word

print 'Building dictionary...',

dictionary = { }

# Notice that you need to have a file named 'dictionary.txt'
# in the same directory as this file. The format is to have
# one word per line, such as the following (of course without
# the # marks):


f = file('dictionary.txt')

# This loop builds the dictionary, where the key is
# the string after calling sort_string(), and the value
# is the list of all 'regular' words (from the dictionary,
# not sorted) that passing to sort_string() returns the key
while True:
        line = f.readline()
        if len(line) == 0:
        line = str.lower(line[:-1]) # convert to lowercase just in case and
                                    # remove the return at the end of the line
        sline = sort_string(line)
        if sline in dictionary:     # this key already exist, add to existing list
                print 'Added %s to key %s' % (line,sline) #for testing
        else:                       # create new key and list
                dictionary[sline] = [line]
                print 'Created key %s for %s' % (sline,line) #for testing

print 'Ready!'

# This loop lets the user input a scrambled word, look for it in
# dictionary, and print all matching unscrambled words.
# If the user types 'quit' then the program ends.
while True:
        lookup = raw_input('Enter a scrambled word : ')

        results = dictionary[sort_string(lookup)]

        for x in results:
                print x,


        if lookup == 'quit':
If you create dictionary.txt as suggested in the comments, it should work fine (assumeing you pass a word that creates a valid key, I'll have to add exceptions later). The problem is when using a large dictionary.txt file (2.9 MB is the size of the dictionary I tested) it always gives an error, specifically:
(Note: ccehimnostyz is for zymotechnics, which is in the large dictionary)
Enter a scrambled word : ccehimnostyz
Traceback (most recent call last):
  File "", line 62, in ?
    results = dictionary[sort_string(lookup)]
KeyError: 'ccehimnostyz'
If you'd like a copy of the dictionary I'm using email me at or leave your email here and I'll send it to you (It's 702.2 KB compressed)

Old 07-25-2005, 07:40 PM   #2
Senior Member
Registered: Dec 2002
Location: Atlantic City, NJ
Distribution: Ubuntu & Arch
Posts: 3,503

Rep: Reputation: 57
A dictionary object does not have an append method. You simply set a key equal to a value so instead of having this:

you would have this:

dictionary[sline] = line
I didn't test all your code but I think thats the problem.

Also, in the sort_string function, you can replace a lot of that code with the built in list function:

word_list = list(word)


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Python: loading page from forum fiomba Linux - Software 13 11-24-2004 09:51 AM
[bash] Put words from file to array mispunt Programming 4 11-04-2004 10:53 AM
Search and Replace: Asian Words to English Words ieeestd802 Linux - Software 0 10-27-2004 07:48 PM
Gnucash .log file loading problem Toonses82 Linux - Software 1 09-16-2004 11:03 PM
How can I catch some "KEY WORDS" from a text file without opening it? yuzuohong Linux - General 1 06-14-2002 09:27 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:20 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration