奇怪的Python KeyError

时间:2017-11-01 21:51:33

标签: python json

我今天一直在研究这个脚本,并在循环数据并将其导入外部数据库方面取得了一些非常好的进展。我试图对我遇到问题的字段进行故障排除,并且它没有多大意义。每当我尝试运行它时,我都会收到以下错误KeyError: 'manufacturer'。如果我注释掉行product_details['manufacturer'] = item['manufacturer'],脚本将按预期运行。

  1. 我已查看过caseSensitivty
  2. 我检查了我的拼写
  3. 我已经确认我从中抽出的json文件有该字段 填写完毕,
  4. 我确认支持DataType(它只是一个字符串);
  5. 不知道还有什么要检查或从这里去的地方(新到python)

    我使用以下 test data

    import json
    
    input_file = open ('data/bestbuy_seo.json')
    json_array = json.load(input_file)
    product_list = []
    
    for item in json_array:
        product_details = {"name": None, "shortDescription": None, "bestSellingRank": None,
        "thumbnailImage": None, "salePrice": None, "manufacturer": None, "url": None,
        "type": None, "image": None, "customerReviewCount": None, "shipping": None,
        "salePrice_range": None, "objectID": None, "categories": [None] }
        product_details['name']                = item['name']
        product_details['shortDescription']    = item['shortDescription']
        product_details['bestSellingRank']     = item['bestSellingRank']
        product_details['thumbnailImage']      = item['thumbnailImage']
        product_details['salePrice']           = item['salePrice']
        product_details['manufacturer']        = item['manufacturer']
        product_details['url']                 = item['url']
        product_details['type']                = item['type']
        product_details['image']               = item['image']
        product_details['customerReviewCount'] = item['customerReviewCount']
        product_details['shipping']            = item['shipping']
        product_details['salePrice_range']     = item['salePrice_range']
        product_details['objectID']            = item['objectID']
        product_details['categories']          = item['categories']
        product_list.append(product_details)
    
    # Let's dump it to the screen to see if it works
    print json.dumps(product_list, indent=4)
    

3 个答案:

答案 0 :(得分:1)

我的猜测是,您数据中的某个项目没有制造商'钥匙套。

替换

项['制造商']

通过

item.get('制造商',无)

或由默认制造商替换无......

答案 1 :(得分:1)

不是手头的问题(项目缺少键manufacturer,或许更多),但由于您只是使用完全相同的键复制字段,因此您可以编写类似这样的内容。另请注意,item.get(key, None)将以产品中None值为代价来消除此错误(因此,如果您希望代码在失败时失败,那么这可能会很糟糕)

import json

input_file = open ('data/bestbuy_seo.json')
json_array = json.load(input_file)
product_list = []

product_keys = ('objectID', 'image', 'thumbnailImage',
                'shortDescription', 'categories', 'manufacturer',
                'customerReviewCount', 'name', 'url', 'shipping',
                'salePrice', 'bestSellingRank', 'type',
                'salePrice_range')

for item in json_array:
    product_list.append(dict((key, item.get(key, None)) for key in product_keys))

# Let's dump it to the screen to see if it works
print json.dumps(product_list, indent=4)

答案 2 :(得分:0)

这有两种方法可以绕过没有密钥的字典。两者都可以工作,但第一个可能更容易使用,并将作为您当前代码的一个下降。

这是使用python的dictionary.get()方法完成此操作的一种方法。 Here is a page with more examples of how it works。这种解决问题的方法受Ian A. Mason import json input_file = open('data/bestbuy_seo.json') json_array = json.load(input_file) product_list = [] for item in json_array: product_details = { 'name': item.get('name', None), 'shortDescription': item.get('shortDescription', None), 'bestSellingRank': item.get('bestSellingRank', None), 'thumbnailImage': item.get('thumbnailImage', None), 'salePrice': item.get('salePrice', None), 'manufacturer': item.get('manufacturer', None), 'url': item.get('url', None), 'type': item.get('type', None), 'image': item.get('image', None), 'customerReviewCount': item.get('customerReviewCount', None), 'shipping': item.get('shipping', None), 'salePrice_range': item.get('salePrice_range', None), 'objectID': item.get('objectID', None), 'categories': item.get('categories', None) } product_list.append(product_details) # Let's dump it to the screen to see if it works print json.dumps(product_list, indent=4) 对当前问题的回答的启发。我根据他的回答改变了你的代码。

import json
from copy import deepcopy

input_file = open('data/bestbuy_seo.json')
json_array = json.load(input_file)
product_list = []
product_details_master = {"name": None, "shortDescription": None, "bestSellingRank": None,
                          "thumbnailImage": None, "salePrice": None, "manufacturer": None, "url": None,
                          "type": None, "image": None, "customerReviewCount": None, "shipping": None,
                          "salePrice_range": None, "objectID": None, "categories": [None]}
for item in json_array:
    product_details_temp = deepcopy(product_details_master)
    try:
        product_details_temp['name'] = item['name']
        product_details_temp['shortDescription'] = item['shortDescription']
        product_details_temp['bestSellingRank'] = item['bestSellingRank']
        product_details_temp['thumbnailImage'] = item['thumbnailImage']
        product_details_temp['salePrice'] = item['salePrice']
        product_details_temp['manufacturer'] = item['manufacturer']
        product_details_temp['url'] = item['url']
        product_details_temp['type'] = item['type']
        product_details_temp['image'] = item['image']
        product_details_temp['customerReviewCount'] = item['customerReviewCount']
        product_details_temp['shipping'] = item['shipping']
        product_details_temp['salePrice_range'] = item['salePrice_range']
        product_details_temp['objectID'] = item['objectID']
        product_details_temp['categories'] = item['categories']
        product_list.append(product_details_temp)
    except KeyError:
        # Add error handling here! Right now if a product does not have all the keys NONE of the current object
        # will be added to the product_list!
        print 'There was a missing key in the json'

# Let's dump it to the screen to see if it works
print json.dumps(product_list, indent=4)

这是使用“请求宽恕而非许可”的第二种方式。在python中的概念。很容易让一个缺少属性的对象失败并继续前进。尝试和期望比一堆if更快。

this

{{1}}