Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default dict keys to avoid KeyError

I'm calling some JSON and parsing relevant data as CSV. I cannot figure out how to fill in the intermediate JSON dict file with default keys, as many are unpopulated. The result is a KeyError as I attempt to parse the content into a CSV.

I'm now receiving a 'NoneType' error for (manufacturer):

import urllib2, json, csv, sys, os, codecs, re  from collections import defaultdict  output = 'bb.csv'  csv_writer = csv.writer(open(output, 'w'))  header = ['sku', 'name', 'description', 'image', 'manufacturer', 'upc', 'department', 'class', 'subclass']  csv_writer.writerow(header)  i=1  while i<101:     print i      bb_url = urllib2.Request("http://api.remix.bestbuy.com/v1/products(sku=*)?show=sku,name,description,image,manufacturer,upc,department,class,subclass&format=json&sort=sku.asc&page=" + str(i) + "&pageSize=100&apiKey=*****************")     bb_json = json.load(urllib2.urlopen(bb_url))      print bb_json      for product in bb_json['products']:         row = []          row.append(product['sku'])         if product['name']:             row.append(str((product['name']).encode('utf-8')))         else:             row.append("")         row.append(str(product.get('description',"")))         row.append(str(product['image'])+ " ")         if product['name']:             row.append(str(product.get('manufacturer',"").encode('utf-8')))         else:             row.append("")         row.append(str(product.get('upc','').encode('utf-8')))         row.append(str((product['department']).encode('utf-8')))         row.append(str((product['class']).encode('utf-8')))         row.append(str((product['subclass']).encode('utf-8')))          csv_writer.writerow(row)      i = i+1 
like image 487
Kenfucious Avatar asked Jul 17 '14 21:07

Kenfucious


People also ask

How do you avoid KeyError in the dictionary?

Avoiding KeyError when accessing Dictionary Key We can avoid KeyError by using get() function to access the key value. If the key is missing, None is returned. We can also specify a default value to return when the key is missing.

How do you resolve KeyError?

The Usual Solution: . If the KeyError is raised from a failed dictionary key lookup in your own code, you can use . get() to return either the value found at the specified key or a default value.

What is a default dict in Python?

The defaultdict is a subdivision of the dict class. Its importance lies in the fact that it allows each new key to be given a default value based on the type of dictionary being created. A defaultdict can be created by giving its declaration an argument that can have three values; list, set or int.

How does default dict work?

A defaultdict works exactly like a normal dict, but it is initialized with a function (“default factory”) that takes no arguments and provides the default value for a nonexistent key. A defaultdict will never raise a KeyError. Any key that does not exist gets the value returned by the default factory.


1 Answers

You can use your_dict.get(key, "default value") instead of directly referencing a key.

like image 123
Stack of Pancakes Avatar answered Sep 30 '22 14:09

Stack of Pancakes