Python json字典到数组

2024-05-29 01:35:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我在理解json字典和数组时遇到了一些问题。我有一个脚本,是从一个网站抓取信息。在

在模型.txt只是一个型号列表,例如

30373
30374
30375

和json_说明.txt是我想要的钥匙清单

^{pr2}$

代码是:

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"

如果我打印出信息,我会得到:

sku 30373
price 9.10
listprice 17.62
issoldout False

这意味着信息[0]是:

^{pr2}$

信息[1]是:

30373
9.10
17.62
False

我想知道有没有办法让我得到这个: 循环1=['sku','30373','price','4.90','listprice','0','issoldout','False'] {cd2>}

info[0] = skuinfo[1] = 30373info[2] = priceinfo[3] = 4.90info[4] = listpriceinfo[5] = 0info[6] = issoldoutinfo[7] = False,然后在下一个循环中使用一个新列表重复该操作。在

我尝试过使用info = json.dumps(info),但这只给出info[0] = [[[[和{}info[2] = spli等等


Tags: importinfotxt信息jsonfalse列表data
1条回答
网友
1楼 · 发布于 2024-05-29 01:35:19

像这样?在

info = []
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.append(description)
            info.append(get_data(0,description))
print info

相关问题 更多 >

    热门问题