【问题标题】:urllib—expressing what piece of JSON Data I desireurllib——表达我想要的 JSON 数据
【发布时间】:2016-07-22 21:24:11
【问题描述】:

这是我在下面的代码中获取的一大段 JSON 数据:

{
  "status": 200,
  "offset": 0,
  "limit": 10,
  "count": 8,
  "total": 8,
  "url": "/v2/dictionaries/ldoce5/entries?headword=extra",
  "results": [
    {
      "datasets": [
        "ldoce5",
        "dictionary"
      ],
      "headword": "extra",
      "homnum": 3,
      "id": "cqAFDjvvYg",
      "part_of_speech": "adverb",
      "senses": [
        {
          "collocation_examples": [
            {
              "collocation": "one/a few etc extra",
              "example": {
                "audio": [
                  {
                    "type": "example",
                    "url": "/v2/dictionaries/assets/ldoce/exa_pron/p008-001627480.mp3"
                  }
                ],
                "text": "I got a few extra in case anyone else decides to come."
              }
            }
          ],
          "definition": [
            "in addition to the usual things or the usual amount"
          ],
          "examples": [
            {
              "audio": [
                {
                  "type": "example",
                  "url": "/v2/dictionaries/assets/ldoce/exa_pron/p008-001627477.mp3"
                }
              ],
              "text": "They need to offer something extra to attract customers."
            }
          ]
        }
      ],
      "url": "/v2/dictionaries/entries/cqAFDjvvYg"
    },
    {
      "datasets": [
        "ldoce5",
        "dictionary"
      ],
      "headword": "extra-",
      "id": "cqAFDk1BDw",
      "part_of_speech": "prefix",
      "pronunciations": [
        {
          "audio": [
            {
              "lang": "American English",
              "type": "pronunciation",
              "url": "/v2/dictionaries/assets/ldoce/us_pron/extra__pre.mp3"
            }
          ],
          "ipa": "ekstrə"
        }
      ],
      "senses": [
        {
          "definition": [
            "outside or beyond"
          ],
          "examples": [
            {
              "audio": [
                {
                  "type": "example",
                  "url": "/v2/dictionaries/assets/ldoce/exa_pron/p008-001832333.mp3"
                }
              ],
              "text": "extragalactic (=outside our galaxy)"
            }
          ]
        }
      ],
      "url": "/v2/dictionaries/entries/cqAFDk1BDw"
    },
    {
      "datasets": [
        "ldoce5",
        "dictionary"
      ],
      "headword": "extra",
      "homnum": 1,
      "id": "cqAFDjpNZQ",
      "part_of_speech": "adjective",
      "pronunciations": [
        {
          "audio": [
            {
              "lang": "British English",
              "type": "pronunciation",
              "url": "/v2/dictionaries/assets/ldoce/gb_pron/extra_n0205.mp3"
            },
            {
              "lang": "American English",
              "type": "pronunciation",
              "url": "/v2/dictionaries/assets/ldoce/us_pron/extra1.mp3"
            }
          ],
          "ipa": "ˈekstrə"
        }
      ],
      "senses": [
        {
          "collocation_examples": [
            {
              "collocation": "an extra ten minutes/three metres etc",
              "example": {
                "audio": [
                  {
                    "type": "example",
                    "url": "/v2/dictionaries/assets/ldoce/exa_pron/p008-001202489.mp3"
                  }
                ],
                "text": "I asked for an extra two weeks to finish the work."
              }
            }
          ],
          "definition": [
            "more of something, in addition to the usual or standard amount or number"
          ],
          "examples": [
            {
              "audio": [
                {
                  "type": "example",
                  "url": "/v2/dictionaries/assets/ldoce/exa_pron/p008-001202484.mp3"
                }
              ],
              "text": "Could you get an extra loaf of bread?"
            }
          ],
          "gramatical_info": {
            "type": "only before noun"
          }
        }
      ],
      "url": "/v2/dictionaries/entries/cqAFDjpNZQ"
    },
    {
      "datasets": [
        "ldoce5",
        "dictionary"
      ],
      "headword": "extra",
      "homnum": 2,
      "id": "cqAFDjsQjH",
      "part_of_speech": "pronoun",
      "senses": [
        {
          "collocation_examples": [
            {
              "collocation": "pay/charge/cost etc extra",
              "example": {
                "audio": [
                  {
                    "type": "example",
                    "url": "/v2/dictionaries/assets/ldoce/exa_pron/p008-001202499.mp3"
                  }
                ],
                "text": "I earn extra for working on Sunday."
              }
            }
          ],
          "definition": [
            "an amount of something, especially money, in addition to the usual, basic, or necessary amount"
          ],
          "synonym": "more"
        }
      ],
      "url": "/v2/dictionaries/entries/cqAFDjsQjH"
    },
    {
      "datasets": [
        "ldoce5",
        "dictionary"
      ],
      "headword": "extra",
      "homnum": 4,
      "id": "cqAFDjyTn8",
      "part_of_speech": "noun",
      "senses": [
        {
          "definition": [
            "something which is added to a basic product or service that improves it and often costs more"
          ],
          "examples": [
            {
              "audio": [
                {
                  "type": "example",
                  "url": "/v2/dictionaries/assets/ldoce/exa_pron/p008-001202524.mp3"
                }
              ],
              "text": "Tinted windows and a sunroof are optional extras(=something that you can choose to have or not)."
            }
          ]
        }
      ],
      "url": "/v2/dictionaries/entries/cqAFDjyTn8"
    },
    {
      "datasets": [
        "ldoce5",
        "dictionary"
      ],
      "headword": "extra virgin",
      "id": "cqAFDmV2Jw",
      "part_of_speech": "adjective",
      "senses": [
        {
          "definition": [
            "extra virgin olive oil comes from olives that are pressed for the first time, and is considered to be the best quality olive oil"
          ]
        }
      ],
      "url": "/v2/dictionaries/entries/cqAFDmV2Jw"
    },
    {
      "datasets": [
        "ldoce5",
        "dictionary"
      ],
      "headword": "extra time",
      "id": "cqAFDmGZyQ",
      "part_of_speech": "noun",
      "senses": [
        {
          "american_equivalent": "overtime",
          "definition": [
            "a period, usually of 30 minutes, added to the end of a football game in some competitions if neither team has won after normal time"
          ],
          "examples": [
            {
              "audio": [
                {
                  "type": "example",
                  "url": "/v2/dictionaries/assets/ldoce/exa_pron/p008-001627835.mp3"
                }
              ],
              "text": "The match went into extra time."
            }
          ],
          "geography": "especially British English",
          "gramatical_examples": [
            {
              "examples": [
                {
                  "audio": [
                    {
                      "type": "example",
                      "url": "/v2/dictionaries/assets/ldoce/exa_pron/p008-001627834.mp3"
                    }
                  ],
                  "text": "Beckham scored in extra time."
                }
              ],
              "pattern": "in extra time"
            }
          ]
        }
      ],
      "url": "/v2/dictionaries/entries/cqAFDmGZyQ"
    },
    {
      "datasets": [
        "ldoce5",
        "dictionary"
      ],
      "headword": "extra-sensory perception",
      "id": "cqAFDm6ceW",
      "part_of_speech": "noun",
      "senses": [
        {
          "definition": [
            "ESP"
          ]
        }
      ],
      "url": "/v2/dictionaries/entries/cqAFDm6ceW"
    }
  ]
}

我想获取并打印 JSON 结果中提供的定义。我不知道如何表达这一点,我得到一个“列表索引必须是整数或切片,而不是 str”错误,因为我的感觉 = data['senses']。

#!/usr/bin/env python

import urllib.request
import json

wp = urllib.request.urlopen("http://api.pearson.com/v2/dictionaries/ldoce5/entries?headword=extra").read().decode('utf8')
jsonData=json.loads(wp)
data=jsonData['results']

for item in data:
    sense = data['senses']
    print(senses['definition'])

【问题讨论】:

    标签: python json api python-3.x urlfetch


    【解决方案1】:

    sense 实际上是一个包含单个元素的列表,一个字典。包含的字典具有您想要的键值对。

    例如:

    for item in data:
        sense = data['senses'][0]
        print(sense['definition'])
    

    【讨论】:

    • 迈克尔,感谢您的洞察力。这有助于我理解代码中的所有错误。最后的循环实际上是这样的: for item in data: print(item['senses'][0]['definition']) 请注意,我也犯了一个错误,即通过表达 data['senses'] 来引用感官正确的项目['senses']
    猜你喜欢
    • 2023-03-20
    • 2016-01-24
    • 2015-08-05
    • 2019-09-17
    • 1970-01-01
    • 1970-01-01
    • 2013-12-23
    • 1970-01-01
    • 2013-01-10
    相关资源
    最近更新 更多