-6

I'm trying to write a program that will open a text file from the web consisting of 10,000 words, then 'clean' the file to get rid of nonsense words, such as 'aa'. I eventually want to use these words to do other things so I want to add the non 'non-sense' words to be put into a new list. Every time i try to run this I run into the error code TypeError: 'function' object is not iterable.

import urllib.request  

def readWordList():  

response = urllib.request.urlopen("http://www.mit.edu/~ecprice/wordlist.10000")
html = response.read()
data = html.decode('utf-8').split()

return data

clean = readWordList() 

def clean(aList):   
    newList = []
    for word in aList: 
        if range(len(word)) >= 2:
            newList.append(word)
    return newList


clean(clean)
2
  • Please fix your indenting, and include your full traceback. Commented Nov 16, 2017 at 22:58
  • clean(clean)? You can't use the same name for both a function and a list.. Commented Nov 16, 2017 at 23:03

3 Answers 3

5

Make up your mind: is clean supposed to be a list or a function? You started as a list, but then replaced that with a function, and then told the function to clean itself. Try this:

dirty_list = readWordList()
def clean(aList):
...

clean(dirty_list)
Sign up to request clarification or add additional context in comments.

Comments

1

You create a variable called clean, immediately overwrite the name by declaring a function with the same name, then pass the function clean to itself.

Either change the function name, or the variable above with the same name.

Comments

1

Firstly you make a variable called clean and then you make a function called clean and finally you tried to use the function in the variable, both called clean. You "destroy" the variable when you defined a function. They must have different names.

Use this:

import urllib.request  

    def readWordList():  

    response = urllib.request.urlopen("http://www.mit.edu/~ecprice/wordlist.10000")
    html = response.read()
    data = html.decode('utf-8').split()

    return data

    to_clean = readWordList() # Use a different name so it won't be replaced later by the function
        def clean(aList):   
        newList = []
        for word in aList: 
            if range(len(word)) >= 2:
                newList.append(word)
        return newList
    clean(to_clean)

Problem solves; now they have different names.

3 Comments

What does this add to the previous answers? Call out your improvements.
@Prune, I think I made a better explanation so he would understand it better. But if you want... I can delete it...
Nope -- I wanted to make sure your improvements are obvious to later users. SGITE (Slowest Gun In The East) is often useful on SO.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.