Note that there are some explanatory texts on larger screens.

plurals
  1. POPython json dictionary to array
    primarykey
    data
    text
    <p>I am having some trouble understanding json dictionary and array. I have a script that is scraping information from a website. </p> <p>models.txt is just a list of model numbers such as</p> <pre><code>30373 30374 30375 </code></pre> <p>and json_descriptions.txt is a list of the keys I want</p> <pre><code>sku price listprice issoldout </code></pre> <p>The code is:</p> <pre><code>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") for 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] for description in descriptionlist: info = description, (get_data(0,description)) print info else: print "product does not exist" </code></pre> <p>If I print out info I get:</p> <pre><code>sku 30373 price 9.10 listprice 17.62 issoldout False </code></pre> <p>so that means info[0] is:</p> <pre><code>sku price listprice issoldout </code></pre> <p>and info[1] is:</p> <pre><code>30373 9.10 17.62 False </code></pre> <p>I would like to know if there is a way that I can have this: loop 1 = <code>['sku','30373','price','4.90','listprice','0','issoldout','False']</code> loop 2 = <code>['sku','30374','price','10.50','listprice','0','issoldout','False']</code></p> <p><code>info[0] = sku</code> <code>info[1] = 30373</code> <code>info[2] = price</code> <code>info[3] = 4.90</code> <code>info[4] = listprice</code> <code>info[5] = 0</code> <code>info[6] = issoldout</code> <code>info[7] = False</code> and then repeat that with a new list for the next loop through.</p> <p>I have tried using <code>info = json.dumps(info)</code> but that just gives <code>info[0] = [[[[</code> and <code>info[1] = """"</code> <code>info[2] = spli</code> and so on</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload