2013-06-11 149 views
2

我收到此錯誤:python字典關鍵錯誤?

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×16 1/2×5 1/2英寸(44.5×41.9×14釐米)「,」74.51.2648「,」4971「 」artifact4972.jpg「,」總體:5 1/2×3 3/4×4英寸(14.0×9.5×10.2釐米)「,」74.51.2592「,」4972「 」artifact4973.jpg「,」總體:6 5/8×7 1/4×1 1/4英寸(16.8×18.4×3。 2釐米)「,」74.51.2594「,」4973「 」artifact4974.jpg「,」H。 (14 x 17.1 x 29.8 cm)「,」74.51.2628「,」4974「 」artifact4975.jpg「,」Overall:10 1/8 7 7(25.7釐米) 「 」74.51.2633「, 」4975「 」artifact4976.jpg「, 」總體:7 1/2 5 11 1/2中(19.1 12.729.2釐米)「,」 74.51。 2637" , 「4976」 「artifact4977.jpg」, 「總體:10 1/2 7 8 1/2吋(26.7 17.821.6釐米)」, 「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'確實不存在的,但我不知道爲什麼 - 我從一個大的CSV文件幾乎6000條記錄中獲取數據的任何建議,不勝感激

+0

'artifact_group.index(artifact)+ 1'可能是你的問題。嘗試刪除「+ 1」。 – Blender

+0

沒有csv就無法知道。打印出「artifact」+ str(artifact_group.index(artifact)+1)',看看是否有'artifact4'。可能沒有。 – Serdalis

+2

'KeyError'表示'artifact4'不存在_對於這6000個記錄_中的一個。如果你想調試它,第一步是查看_which one_。例如,如果您只是在'for'循環中打印(artifact_group)'。在異常之前打印的最後一個是有問題的那個。 – abarnert

回答

3

如果你遇到這樣的情況該CSV的第四列具有相同的!值作爲較早的一列中,index會產生較早的比賽和artifact4將永遠不會被填充用這個代替:

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

很好的解釋,爲什麼它可能會失敗。更一般地說,你幾乎不應該試圖通過搜索來恢復索引。如果你已經有了索引,就使用它。如果你還沒有,使用'enumerate',現在你就可以做到了。 – abarnert

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'}] 

如果你真的想建立各行快譯通自己,這是很難得錯的,如果你使用字典理解。

的聲明結構強烈建議您適當地想一想。如果你知道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)} 

...而不是試圖恢復索引通過搜索。