【发布时间】:2011-06-23 00:58:05
【问题描述】:
我正在尝试使用两个键构建字典,但在分配项目时出现 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]' 或 ' 时构建 dict 没有问题allProducts[index]',但不能同时使用。
我错过了什么?
【问题讨论】:
标签: python dictionary multidimensional-array