Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
436 views
in Technique[技术] by (71.8m points)

python - 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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...