我想用兩個鍵構建一個字典,但在分配項目時出現KeyError。當單獨使用每個鍵時,我不會看到錯誤,而且語法看起來非常簡單,所以我很難過。在Python中構建多維字典時出現KeyError
searchIndices = ['Books', 'DVD']
allProducts = {}
for index in searchIndices:
res = amazon.ItemSearch(Keywords = entity, SearchIndex = index, ResponseGroup = 'Large', ItemPage = 1, Sort = "salesrank", Version = '2010-11-01')
products = feedparser.parse(res)
for x in range(10):
allProducts[index][x] = { 'price' : products['entries'][x]['formattedprice'],
'url' : products['entries'][x]['detailpageurl'],
'title' : products['entries'][x]['title'],
'img' : products['entries'][x]['href'],
'rank' : products['entries'][x]['salesrank']
}
我不認爲問題在於feedparser(其轉化XML與dict)或與我從亞馬遜獲得的結果,因爲我沒有問題,或者使用「allProducts時建立的字典[X ]'或'allProducts [index]',但不是兩者。
我錯過了什麼?
漂亮,我是新來的Python和忘記它不autovivify。將註冊並upvote你,謝謝! – kasceled 2011-02-07 21:35:15