首页 > 解决方案 > TypeError:类型'type'的参数在python中不可迭代

问题描述

我试图遍历 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)

标签: pythonpython-3.xdictionaryexceptiontypeerror

解决方案


if word in dict:

dict是字典类型,类似于list, int,float等。

不要dict用作变量名。


推荐阅读