python - 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)
解决方案
if word in dict:
dict
是字典类型,类似于list
, int
,float
等。
不要dict
用作变量名。
推荐阅读
- python - Jupyterlab Notebooks 中安装和导入的区别
- facebook - Facebook 在打开对话时自动发送消息
- c - c- 当一个表达式有逗号操作符时,它是如何计算的?
- excel - 使用字典方法存储值
- java - Hibernate 升级后连接增加
- swift - 拖动到另一个字段时如何强制 NSTokenField *move* 标记?
- typescript - TypeScript 错误 - Redux 中间件属性 0
- nginx - yii2 basic 使用 nginx 重写 url
- java - ObjectBox 查询 ToOne 和 ToMany With Or 包含搜索词
- flutter - 禁用自定义日历抖动的日期选择