使用python从嵌套的json检索数据

时间:2018-09-20 13:38:36

标签: python json

我正在尝试从多个嵌套的json检索数据并将其放入csv文件中。代码是:

    for script in scripts:
    if 'offerInfo' in script.text:
        offer_data = script.text[144:][:-1]
        json_data = json.loads(offer_data)
        if str(id) not in json_data['data']:
            return
        offer_name = json_data['data'][str(id)]['offerName']
        offer_price = json_data['data'][str(id)]['price']
        offer_terms = ('https:' + json_data['data'][str(id)]['terms'][0])
        package_products = json_data['data'][str(id)]['products'][1]['name']
        return [id, offer_name, offer_price, offer_terms, offer_products ]

它可以工作,但是问题是,一些json文件不完整,我尝试撤消的miss字段。在这种情况下,它以IndexError: list index out of range结尾。我如何将其更改为仅忽略缺少的字段?

这是json文件的示例:

{
"list":[
"55"
],
"data":{
"55":{
"id":"55",
"offerName":"Offer 55",
"products":[
{
"name":"Product 1",
"type":"software",
"category":"Internet",
"id":"Product_1",
"description":"Product 1 description":[
"\/\/www.url.com\/products\/product_1_description.pdf"
]
},
{
"name":"Product 2",
"type":"",
"category":"hardware",
"id":"Product 2",
"description":"Product 2 description",
"documents":[
"\/\/www.url.com\/products\/product_2_description.pdf"
]
}

1 个答案:

答案 0 :(得分:0)

您可以仅使用try / except块修改代码,因此可以忽略脚本抛出的任何错误,尽管在不同情况下我不建议这样做。

for script in scripts:
    if 'offerInfo' in script.text:
        offer_data = script.text[144:][:-1]
            try:
                json_data = json.loads(offer_data)
                if str(id) not in json_data['data']:
                    return
                offer_name = json_data['data'][str(id)]['offerName']
                offer_price = json_data['data'][str(id)]['price']
                offer_terms = ('https:' + json_data['data'][str(id)]['terms'][0])
                package_products = json_data['data'][str(id)]['products'][1]['name']
                return [id, offer_name, offer_price, offer_terms, offer_products ]
            except IndexError:
                pass

这样,它将跳过所有不存在的条目。