如何使用Python读取JSON数据?

时间:2019-02-22 21:53:44

标签: python json

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

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 个答案:

答案 0 :(得分:3)

属性和元素是不同的东西。另外,您的数据没有len元素,因此我假设您希望内置的Python len()函数获取句子列表的长度。

那么你在哪里:

json_data.sentences.len

您要

len(json_data["sentences"])