代码之家  ›  专栏  ›  技术社区  ›  Shamoon

如何使用Python读取JSON数据?

  •  -1
  • Shamoon  · 技术社区  · 6 年前

    这是一个非常简单的问题,但我找不到一个简单的答案:

    for petid in X['PetID']:
        sentiment_file = datapath + '/train_sentiment/' + petid + '.json'
        if os.path.isfile(sentiment_file):
            json_data = json.loads(open(sentiment_file).read())
            print(petid, sentiment_file, json_data.sentences.len)
    

    这是我的准则。我的JSON文件有:

    {
      "sentences": [
        {
          "text": {
            "content": "Fenny was rescued from Old Klang Rd market when she was only 2 months old and I have fostered her since then.",
            "beginOffset": -1
          },
          "sentiment": {
            "magnitude": 0.1,
            "score": -0.1
          }
        },
        {
          "text": {
            "content": "She is 5 mths old now and I hope she can get a good home.",
            "beginOffset": -1
          },
          "sentiment": {
            "magnitude": 0.7,
            "score": 0.7
          }
        },
        {
          "text": {
            "content": "She looks like a mixed breed of local mongrel and Dalmation.",
            "beginOffset": -1
          },
          "sentiment": {
            "magnitude": 0.1,
            "score": 0.1
          }
        },
        {
          "text": {
            "content": "She is a very quiet girl and does not make too much noise, that makes her a very good companion for children or elderly couple.",
            "beginOffset": -1
          },
          "sentiment": {
            "magnitude": 0.9,
            "score": 0.9
          }
        },
        {
          "text": {
            "content": "Please call Mrs Lai of Paws Mission for more adoption details.",
            "beginOffset": -1
          },
          "sentiment": {
            "magnitude": 0,
            "score": 0
          }
        }
      ],
      "tokens": [],
      "entities": [
        {
          "name": "Fenny",
          "type": "PERSON",
          "metadata": {},
          "salience": 0.7863105,
          "mentions": [
            {
              "text": {
                "content": "Fenny",
                "beginOffset": -1
              },
              "type": "PROPER"
            },
            {
              "text": {
                "content": "girl",
                "beginOffset": -1
              },
              "type": "COMMON"
            }
          ]
        },
        {
          "name": "market",
          "type": "OTHER",
          "metadata": {},
          "salience": 0.08208243,
          "mentions": [
            {
              "text": {
                "content": "market",
                "beginOffset": -1
              },
              "type": "COMMON"
            }
          ]
        },
        {
          "name": "Old Klang Rd",
          "type": "LOCATION",
          "metadata": {},
          "salience": 0.04985573,
          "mentions": [
            {
              "text": {
                "content": "Old Klang Rd",
                "beginOffset": -1
              },
              "type": "PROPER"
            }
          ]
        },
        {
          "name": "home",
          "type": "LOCATION",
          "metadata": {},
          "salience": 0.013762235,
          "mentions": [
            {
              "text": {
                "content": "home",
                "beginOffset": -1
              },
              "type": "COMMON"
            }
          ]
        },
        {
          "name": "Lai of Paws Mission",
          "type": "PERSON",
          "metadata": {},
          "salience": 0.011584155,
          "mentions": [
            {
              "text": {
                "content": "Lai of Paws Mission",
                "beginOffset": -1
              },
              "type": "PROPER"
            },
            {
              "text": {
                "content": "Mrs",
                "beginOffset": -1
              },
              "type": "COMMON"
            }
          ]
        },
        {
          "name": "breed",
          "type": "OTHER",
          "metadata": {},
          "salience": 0.01073034,
          "mentions": [
            {
              "text": {
                "content": "breed",
                "beginOffset": -1
              },
              "type": "COMMON"
            }
          ]
        },
        {
          "name": "mongrel",
          "type": "OTHER",
          "metadata": {},
          "salience": 0.009851005,
          "mentions": [
            {
              "text": {
                "content": "mongrel",
                "beginOffset": -1
              },
              "type": "COMMON"
            }
          ]
        },
        {
          "name": "companion",
          "type": "PERSON",
          "metadata": {},
          "salience": 0.00740921,
          "mentions": [
            {
              "text": {
                "content": "companion",
                "beginOffset": -1
              },
              "type": "COMMON"
            }
          ]
        },
        {
          "name": "children",
          "type": "PERSON",
          "metadata": {},
          "salience": 0.00740921,
          "mentions": [
            {
              "text": {
                "content": "children",
                "beginOffset": -1
              },
              "type": "COMMON"
            }
          ]
        },
        {
          "name": "couple",
          "type": "PERSON",
          "metadata": {},
          "salience": 0.00740921,
          "mentions": [
            {
              "text": {
                "content": "couple",
                "beginOffset": -1
              },
              "type": "COMMON"
            }
          ]
        },
        {
          "name": "Dalmation",
          "type": "PERSON",
          "metadata": {},
          "salience": 0.0058382954,
          "mentions": [
            {
              "text": {
                "content": "Dalmation",
                "beginOffset": -1
              },
              "type": "PROPER"
            }
          ]
        },
        {
          "name": "adoption details",
          "type": "OTHER",
          "metadata": {},
          "salience": 0.00517885,
          "mentions": [
            {
              "text": {
                "content": "adoption details",
                "beginOffset": -1
              },
              "type": "COMMON"
            }
          ]
        },
        {
          "name": "noise",
          "type": "OTHER",
          "metadata": {},
          "salience": 0.0025788217,
          "mentions": [
            {
              "text": {
                "content": "noise",
                "beginOffset": -1
              },
              "type": "COMMON"
            }
          ]
        }
      ],
      "documentSentiment": {
        "magnitude": 1.9,
        "score": 0.3
      },
      "language": "en",
      "categories": []
    }
    

    但是,我得到了一个错误:

    AttributeError: 'dict' object has no attribute 'sentences'
    
    1 回复  |  直到 6 年前
        1
  •  3
  •   kindall    6 年前

    len 元素,所以我假设您需要内置的Python len()

    因此,您可以:

    json_data.sentences.len
    

    你想要:

    len(json_data["sentences"])