Python嵌套字典数据提取

时间:2020-10-30 10:26:30

标签: python dictionary

我有如下字典:

{
  "result" : [{
      "conf" : 1.000000,
      "end" : 105.570000,
      "start" : 104.970000,
      "word" : "today"
    }, {
      "conf" : 1.000000,
      "end" : 106.320000,
      "start" : 106.080000,
      "word" : "yes"
    }, {
      "conf" : 1.000000,
      "end" : 106.590000,
      "start" : 106.320000,
      "word" : "sir"
    }],
  "text" : "today yes sir"
}

我想从位于结果内的内部字典中提取startword变量。 我该怎么做呢?任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

提供您的字典:

my_dict = {
  "result" : [{
      "conf" : 1.000000,
      "end" : 105.570000,
      "start" : 104.970000,
      "word" : "today"
    }, {
      "conf" : 1.000000,
      "end" : 106.320000,
      "start" : 106.080000,
      "word" : "yes"
    }, {
      "conf" : 1.000000,
      "end" : 106.590000,
      "start" : 106.320000,
      "word" : "sir"
    }],
  "text" : "today yes sir"
}

然后

start = my_dict['result'][0]['start']
word = my_dict['result'][0]['word']

答案 1 :(得分:0)

这是您给的字典:

wget -P /etc/yum.repos.d/ https://nexus.lab.fiware.org/repository/raw/public/repositories/el/7/x86_64/fiware-release.repo

--2020-10-29 11:40:54--  https://nexus.lab.fiware.org/repository/raw/public/repositories/el/7/x86_64/fiware-release.repo
Resolving nexus.lab.fiware.org (nexus.lab.fiware.org)... 109.234.71.209
Connecting to nexus.lab.fiware.org (nexus.lab.fiware.org)|109.234.71.209|:443... connected.
HTTP request sent, awaiting response... 404 public/repositories/el/7/x86_64/fiware-release.repo
2020-10-29 11:40:54 ERROR 404: public/repositories/el/7/x86_64/fiware-release.repo

这可以获取所有的“单词”和“开始”值:

dict ={
   "result" : [{
       "conf" : 1.000000,
       "end" : 105.570000,
       "start" : 104.970000,
       "word" : "today"
     }, {
       "conf" : 1.000000,
       "end" : 106.320000,
       "start" : 106.080000,
       "word" : "yes"
    }, {
       "conf" : 1.000000,
       "end" : 106.590000,
       "start" : 106.320000,
       "word" : "sir"
     }],
   "text" : "today yes sir"
}
相关问题