【发布时间】:2018-04-30 15:12:52
【问题描述】:
我正在尝试编写一个程序,该程序将从网络上打开一个包含 10,000 个单词的文本文件,然后“清理”该文件以删除无意义的单词,例如“aa”。我最终想用这些词做其他事情,所以我想将非“无意义”的词添加到新列表中。每次我尝试运行它时都会遇到错误代码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)
【问题讨论】:
-
请修正缩进,并包含完整的回溯。
-
clean(clean)?函数和列表不能使用相同的名称..
标签: python python-3.x file error-handling