python字典键错误?

时间:2013-06-11 20:59:57

标签: python dictionary

我收到此错误:

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 in。(44.5 x 41.9 x 14 cm)”,“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 in。(14 x 17.1 x 29.8 cm)”,“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 in。(19.1 12.7 29.2 cm)”,“74.51.2637”,“4976” “artifact4977.jpg”,“总体:10 1/2 7 8 1/2 in。(26.7 17.8 21.6 cm)”,“74.51.2819”,“4977” “artifact4978.jpg”,“H.6 3/8 x 14 1/2 x 5 1/4 in。(16.2 x 36.8 x 13.3 cm)”,“74.51.2831”,“4978”

我知道KeyError表示'artifact4'不存在,但我不知道为什么 - 我从一个包含近6000条记录的大型CSV文件中获取数据。任何建议都非常感谢!

2 个答案:

答案 0 :(得分:3)

如果您遇到CSV的第四列与之前的列之一具有相同值的情况,则index将生成较早的匹配,并且永远不会填充artifact4。请改用:

 for i, artifact in enumerate(artifact_group):
     art_temp_dict["artifact"+str(i+1)] = artifact

答案 1 :(得分:3)

您可以使用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'}]

如果你真的想自己构建每一行,那么如果你使用dict理解就更难弄错。

声明性结构强烈鼓励您正确地思考这个问题。如果您了解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)}

...而不是通过搜索来尝试恢复索引。