亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 Python 從 JSON 文件中提取列表中所有對象的特定字段

如何使用 Python 從 JSON 文件中提取列表中所有對象的特定字段

弒天下 2023-06-20 17:13:26
我正在嘗試為列表中的每個對象提取一個特定的字段,它在 JSON 中的值,到目前為止,我已經開始一個一個地獲取它們,但是如果有更多的對象具有相同的字段,它需要提取所有這些字段被添加到 JSON 中。這是 JSON:[{    "took" : 1023,    "timed_out" : false,    "_shards" : {      "total" : 5,      "successful" : 5,      "skipped" : 0,      "failed" : 0    },    "hits" : {      "total" : 1,      "max_score" : 114.88808,      "hits" : [        {          "_index" : 1,          "_type" : "doc",          "_id" : 1,          "_score" : 114.88808,          "_source" : {            "message" : "Error something happened"          }        }      ]    }  },  {    "took" : 1023,    "timed_out" : false,    "_shards" : {      "total" : 5,      "successful" : 5,      "skipped" : 0,      "failed" : 0    },    "hits" : {      "total" : 1,      "max_score" : 114.88808,      "hits" : [        {          "_index" : 2,          "_type" : "doc",          "_id" : 2,          "_score" : 114.88808,          "_source" : {            "message" : "Something else"          }        }      ]    }  }]我正在嘗試從兩個對象的字段中獲取值message,就我提到的而言,我設法像這樣一個一個地完成:data = json.loads(open('test.json').read())extracted_data = data[0]['hits']['hits'][0]['_source']['message']
查看完整描述

3 回答

?
富國滬深

TA貢獻1790條經驗 獲得超9個贊

以下


data = [{

    "took" : 1023,

    "timed_out" : 'false',

    "_shards" : {

      "total" : 5,

      "successful" : 5,

      "skipped" : 0,

      "failed" : 0

    },

    "hits" : {

      "total" : 1,

      "max_score" : 114.88808,

      "hits" : [

        {

          "_index" : 1,

          "_type" : "doc",

          "_id" : 1,

          "_score" : 114.88808,

          "_source" : {

            "message" : "Error something happened"

          }

        }

      ]

    }

  },

  {

    "took" : 1023,

    "timed_out" : 'false',

    "_shards" : {

      "total" : 5,

      "successful" : 5,

      "skipped" : 0,

      "failed" : 0

    },

    "hits" : {

      "total" : 1,

      "max_score" : 114.88808,

      "hits" : [

        {

          "_index" : 2,

          "_type" : "doc",

          "_id" : 2,

          "_score" : 114.88808,

          "_source" : {

            "message" : "Something else"

          }

        }

      ]

    }

  }

]

messages = [x['hits']['hits'][0]['_source']['message'] for x in data]

print(messages)

輸出


['Error something happened', 'Something else']


查看完整回答
反對 回復 2023-06-20
?
繁星coding

TA貢獻1797條經驗 獲得超4個贊

嘗試這個


res = [y['_source']['message'] for x in data for y in x['hits']['hits']]

print(res)

輸出:


['Error something happened', 'Something else']


查看完整回答
反對 回復 2023-06-20
?
呼啦一陣風

TA貢獻1802條經驗 獲得超6個贊

一個簡單的 for 循環就可以解決問題:


for d in data :

    extracted_data.append(d['hits']['hits'][0]['_source']['message'])


查看完整回答
反對 回復 2023-06-20
  • 3 回答
  • 0 關注
  • 281 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號