Python json dictionary to array -
i having trouble understanding json dictionary , array. have script scraping information website.
models.txt list of model numbers such as
30373 30374 30375
and json_descriptions.txt list of keys want
sku price listprice issoldout
the code is:
import urllib import re import json modelslist = open("models.txt").read() modelslist = modelslist.split("\n") descriptionlist = open("json_descriptions.txt").read() descriptionlist = descriptionlist.split("\n") model in modelslist: htmltext = urllib.urlopen("http://dx.com/p/getproductinforealtime?skus="+model) htmltext = json.load(htmltext) if htmltext['success'] == true: def get_data(dict_index, key): return htmltext[u"data"][dict_index][key] description in descriptionlist: info = description, (get_data(0,description)) print info else: print "product not exist"
if print out info get:
sku 30373 price 9.10 listprice 17.62 issoldout false
so means info[0] is:
sku price listprice issoldout
and info[1] is:
30373 9.10 17.62 false
i know if there way can have this: loop 1 = ['sku','30373','price','4.90','listprice','0','issoldout','false']
loop 2 = ['sku','30374','price','10.50','listprice','0','issoldout','false']
info[0] = sku
info[1] = 30373
info[2] = price
info[3] = 4.90
info[4] = listprice
info[5] = 0
info[6] = issoldout
info[7] = false
, repeat new list next loop through.
i have tried using info = json.dumps(info)
gives info[0] = [[[[
, info[1] = """"
info[2] = spli
, on
like this?
info = [] model in modelslist: htmltext = urllib.urlopen("http://dx.com/p/getproductinforealtime?skus="+model) htmltext = json.load(htmltext) if htmltext['success'] == true: def get_data(dict_index, key): return htmltext[u"data"][dict_index][key] description in descriptionlist: info.append(description) info.append(get_data(0,description)) print info
Comments
Post a Comment