TypeError:类型“ type”的参数在python中不可迭代

时间:2019-03-01 05:06:31

标签: python python-3.x dictionary exception typeerror

我试图遍历tsv文件的每一行,并检查字典中是否存在每个单词。该功能可以从每一行的每个内容中提取单词,但是在查找字典中是否存在单词时会失败。

dic = {'films': '0', 'adapted': '1', 'from': '2', 'comic': '3', 'books': '4'}

csv文件格式= [1\tcontent]

def extract_feature(filename):
    with open(filename) as tsv:
        reader = csv.reader(tsv, delimiter = "\t")
        for row in reader:
            for word in row[1].split():
                if word in dict:
                    print(word)

1 个答案:

答案 0 :(得分:0)

if word in dict:

dict是字典类型,类似于listintfloat等。

请勿使用dict作为变量名。