[英]How can I read JSON data with Python?
這是一個非常簡單的問題,但我找不到一個簡單的答案:
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'
屬性和元素是不同的東西。 另外,您的數據沒有len
元素,因此我假設您希望內置的Python len()
函數獲取句子列表的長度。
所以你有:
json_data.sentences.len
你要:
len(json_data["sentences"])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.