我收到此错误:
Traceback (most recent call last):
File "/Users/Rose/Documents/workspace/METProjectFOREAL/src/test_met4.py", line 79, in <module>
table_list.append(table_template % art_temp_dict)
KeyError: 'artifact4'
从下面的代码:
artifact_groups = grouper(4, html_list, "")
for artifact_group in artifact_groups:
art_temp_dict={}
for artifact in artifact_group:
art_temp_dict["artifact"+str(artifact_group.index(artifact)+1)] = artifact
table_list.append(table_template % art_temp_dict)
以下是CSV的示例:
"artifact4971.jpg","H. 17 1/2 x 16 1/2 x 5 1/2英寸(44.5 x 41.9 x 14厘米)“,"74.51.2648","4971”"artifact4972.jpg",“整体:5 1/2 x 3 3/4 x 4英寸(14.0 x 9.5 x 10.2厘米)“,"74.51.2592","4972”"artifact4973.jpg",“整体:6 5/8 x 7 1/4 x 1 1/4英寸(16.8 x 18.4 x 3.2厘米)“,"74.51.2594","4973”"artifact4974.jpg","H. 5 1/2 x 6 3/4 x 11 3/4英寸(14 x 17.1 x 29.8厘米)“,"74.51.2628","4974”"artifact4975.jpg",“总体: 10 1/8 7 7英寸(25.7厘米)“,"74.51.2633","4975”"artifact4976.jpg",“总体:7 1/2 5 11 1/2英寸(19.1 12.7 29.2厘米)“,"74.51.2637","4976”"artifact4977.jpg",“总体: 10 1/2 7 8 1/2英寸(26.7 17.8 21.6厘米)“,"74.51.2819","4977”"artifact4978.jpg","H. 6 3/8 x 14 1/2 x 5 1/4英寸。(16.2 x 36.8 x 13.3厘米)“,"74.51.2831","4978”
我知道KeyError表示'artifact4‘不存在,但我不知道为什么--我从一个包含近6,000条记录的大型CSV文件中获取数据。非常感谢您的任何建议!
发布于 2013-06-12 05:10:32
如果您曾经遇到过CSV的第四列与前一列具有相同值的情况,则index
将生成较早的匹配,并且artifact4
将永远不会被填充。改用下面的代码:
for i, artifact in enumerate(artifact_group):
art_temp_dict["artifact"+str(i+1)] = artifact
发布于 2013-06-12 05:17:38
您可以使用csv.DictReader
而不是csv.reader
,然后尝试从每一行生成一个dict
,从而使这一过程变得更简单:
>>> s='''"artifact4971.jpg","H. 17 1/2 x 16 1/2 x 5 1/2 in. (44.5 x 41.9 x 14 cm)","74.51.2648","4971"
... "artifact4972.jpg","Overall: 5 1/2 x 3 3/4 x 4 in. (14.0 x 9.5 x 10.2 cm)","74.51.2592","4972"
... "artifact4973.jpg","Overall: 6 5/8 x 7 1/4 x 1 1/4 in. (16.8 x 18.4 x 3.2 cm)","74.51.2594","4973"'''
>>> reader = csv.DictReader(s.splitlines(),
... ('artifact1', 'artifact2', 'artifact3', 'artifact4'))
>>> list(reader)
[{'artifact1': 'artifact4971.jpg',
'artifact2': 'H. 17 1/2 x 16 1/2 x 5 1/2 in. (44.5 x 41.9 x 14 cm)',
'artifact3': '74.51.2648',
'artifact4': '4971'},
{'artifact1': 'artifact4972.jpg',
'artifact2': 'Overall: 5 1/2 x 3 3/4 x 4 in. (14.0 x 9.5 x 10.2 cm)',
'artifact3': '74.51.2592',
'artifact4': '4972'},
{'artifact1': 'artifact4973.jpg',
'artifact2': 'Overall: 6 5/8 x 7 1/4 x 1 1/4 in. (16.8 x 18.4 x 3.2 cm)',
'artifact3': '74.51.2594',
'artifact4': '4973'}]
如果你真的想自己构建每一行字典,那么如果你使用字典理解就更难出错了。
声明性结构强烈鼓励您正确地考虑这一点。如果你了解enumerate
,你可能会这样写:
art_temp_dict={'artifact'+str(i+1): artifact
for i, artifact in enumerate(artifact_group)}
…如果不是这样,就像这样--更丑陋,但仍然正确:
art_temp_dict={'artifact'+str(i+1): artifact_group[i]
for i in len(artifact_group)}
…而不是试图通过搜索来恢复索引。
https://stackoverflow.com/questions/17053730
复制相似问题