-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)
Prune
  • 76,765
  • 14
  • 60
  • 81
AngusOld
  • 43
  • 1
  • 7

3 Answers3

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)
Prune
  • 76,765
  • 14
  • 60
  • 81
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.

Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
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.

Ender Look
  • 2,303
  • 2
  • 17
  • 41
  • What does this add to the previous answers? Call out your improvements. – Prune Nov 16 '17 at 23:12
  • @Prune, I think I made a better explanation so he would understand it better. But if you want... I can delete it... – Ender Look Nov 16 '17 at 23:14
  • Nope -- I wanted to make sure your improvements are obvious to later users. SGITE (Slowest Gun In The East) is often useful on SO. – Prune Nov 16 '17 at 23:15