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)
Firstly you make a variable called
clean
and then you make a function calledclean
and finally you tried to use the function in the variable, both calledclean
. You "destroy" the variable when you defined a function. They must have different names.Use this:
Problem solves; now they have different names.
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:You create a variable called
clean
, immediately overwrite the name by declaring a function with the same name, then pass the functionclean
to itself.Either change the function name, or the variable above with the same name.