TypeError: 'function' object is not iterab

2019-09-30 17:33发布

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)

3条回答
仙女界的扛把子
2楼-- · 2019-09-30 18:12

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.

查看更多
Melony?
3楼-- · 2019-09-30 18:19

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)
查看更多
男人必须洒脱
4楼-- · 2019-09-30 18:19

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.

查看更多
登录 后发表回答