Python中文
首页
教程
问答
标签
搜索
登录
注册
TypeError:“generator”对象没有属性“getitem”
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我写了一个生成函数,它应该返回一个字典。但是,当我试图打印一个字段时,会出现以下错误</p> <pre><code>print row2['SearchDate'] TypeError: 'generator' object has no attribute '__getitem__' </code></pre> <p>这是我的密码</p> <pre><code>from csv import DictReader import pandas as pd import numpy as np def genSearch(SearchInfo): for row2 in DictReader(open(SearchInfo)): yield row2 train = 'minitrain.csv' SearchInfo = 'SearchInfo.csv' row2 = {'SearchID': -1} for row1 in DictReader(open(train)): if 'SearchID' in row1 and 'SearchID' in row2 and row1['SearchID'] == row2['SearchID']: x = deepcopy( row1 ) #x['SearchDate'] = row2['percent'] x.update(row2) print 'new' print x else: #call your generator row2 = genSearch(SearchInfo) print row2['SearchDate'] </code></pre>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>生成器返回一个迭代器,显式地需要对其调用next。</p> <p>最后一行代码应该是-</p> <pre><code>rows_generator = genSearch(SearchInfo) row2 = next(rows_generator, None) print row2['SearchDate'] </code></pre> <p>理想情况下,我们在循环中使用迭代器,它会自动为我们执行相同的操作。</p>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
如何找到类似于How'matplotlib.pyplot.gcf()`works?
9 回答
如何找到类字段的定义?
4 回答
如何找到精灵在团队中的位置?
8 回答
如何找到素数,但有错误。我找不到你
9 回答
如何找到素数(Python)
1 回答
如何找到索引i右侧的不同值
1 回答
如何找到索引Numpy数组时将折叠哪些轴?
7 回答
如何找到索引中的值,在列表中增加值?
6 回答
如何找到纬度/经度/高度点之间的三维距离?
2 回答
如何找到线和numpy meshgrid生成的曲面之间的交点?
8 回答
如何找到线段上距任意点最近的点?
4 回答
如何找到组中所有可能的子组
7 回答
如何找到组内值之间的最小差异
9 回答
如何找到经过训练的朴素贝叶斯分类器用于决策的单词?
6 回答
如何找到给selenium webdriver对象的文件夹名?
2 回答
如何找到给出最佳分数的列车测试分割的最佳随机状态值?
9 回答
如何找到给定Python发行版提供的模块?
2 回答
如何找到给定subversion工作副本的根文件夹
6 回答
如何找到给定一维阵列中的所有峰值?
9 回答
如何找到给定列表中的字符串组合,这些字符串加起来就是某个字符串(没有外部库)
3 回答