我对python很新。我正在学习它。
我有json文件,其中包含以下json数据。
{
"document_tone": {
"tone_categories": [
{
"category_id": "emotion_tone",
"tones": [
{
"tone_name": "Anger",
"score": 0.041202,
"tone_id": "anger"
},
{
"tone_name": "Disgust",
"score": 0.054272,
"tone_id": "disgust"
},
{
"tone_name": "Fear",
"score": 0.080706,
"tone_id": "fear"
},
{
"tone_name": "Joy",
"score": 0.813125,
"tone_id": "joy"
},
{
"tone_name": "Sadness",
"score": 0.155878,
"tone_id": "sadness"
}
],
"category_name": "Emotion Tone"
},
{
"category_id": "writing_tone",
"tones": [
{
"tone_name": "Analytical",
"score": 0.0,
"tone_id": "analytical"
},
{
"tone_name": "Confident",
"score": 0.0,
"tone_id": "confident"
},
{
"tone_name": "Tentative",
"score": 0.0,
"tone_id": "tentative"
}
],
"category_name": "Writing Tone"
},
{
"category_id": "social_tone",
"tones": [
{
"tone_name": "Openness",
"score": 0.028,
"tone_id": "openness_big5"
},
{
"tone_name": "Conscientiousness",
"score": 0.314,
"tone_id": "conscientiousness_big5"
},
{
"tone_name": "Extraversion",
"score": 0.944,
"tone_id": "extraversion_big5"
},
{
"tone_name": "Agreeableness",
"score": 0.982,
"tone_id": "agreeableness_big5"
},
{
"tone_name": "Emotional Range",
"score": 0.865,
"tone_id": "neuroticism_big5"
}
],
"category_name": "Social Tone"
}
]
}
}
首先,我想提取以下字段,在提取字段后,我需要“tone_name”,得分最高。
"tones": [
{
"tone_name": "Anger",
"score": 0.041202,
"tone_id": "anger"
},
{
"tone_name": "Disgust",
"score": 0.054272,
"tone_id": "disgust"
},
{
"tone_name": "Fear",
"score": 0.080706,
"tone_id": "fear"
},
{
"tone_name": "Joy",
"score": 0.813125,
"tone_id": "joy"
},
{
"tone_name": "Sadness",
"score": 0.155878,
"tone_id": "sadness"
}
]
我的输出应该是:欢乐
我的代码是
import json
with open('data.json','r') as f:
for line in f:
line = line.strip()
print line
parsedJson = json.loads(line)
print parsedJson
for tone in parsedJson['document_tone']['tone_categories'][0]['tones']:
print(tone['tone_name'])
我的data.json文件包含
"{\n \"document_tone\": {\n \"tone_categories\": [\n {\n
\"category_id\": \"emotion_tone\", \n \"tones\": [\n
{\n \"tone_name\": \"Anger\", \n \"score\":
0.372974, \n \"tone_id\": \"anger\"\n }, \n
{\n \"tone_name\": \"Disgust\", \n \"score\":
0.114389, \n \"tone_id\": \"disgust\"\n }, \n
{\n \"tone_name\": \"Fear\", \n \"score\":
0.083108, \n \"tone_id\": \"fear\"\n }, \n
{\n \"tone_name\": \"Joy\", \n \"score\":
0.028716, \n \"tone_id\": \"joy\"\n }, \n
{\n \"tone_name\": \"Sadness\", \n \"score\":
0.461562, \n \"tone_id\": \"sadness\"\n }\n
], \n \"category_name\": \"Emotion Tone\"\n }, \n
{\n \"category_id\": \"writing_tone\", \n \"tones\":
[\n {\n \"tone_name\": \"Analytical\", \n
\"score\": 0.722, \n \"tone_id\": \"analytical\"\n
}, \n {\n \"tone_name\": \"Confident\", \n
\"score\": 0.0, \n \"tone_id\": \"confident\"\n
}, \n {\n \"tone_name\": \"Tentative\", \n
\"score\": 0.0, \n \"tone_id\": \"tentative\"\n
}\n ], \n \"category_name\": \"Writing Tone\"\n },
\n {\n \"category_id\": \"social_tone\", \n
\"tones\": [\n {\n \"tone_name\": \"Openness\",
\n \"score\": 0.015, \n \"tone_id\":
\"openness_big5\"\n }, \n {\n
\"tone_name\": \"Conscientiousness\", \n \"score\": 0.045,
\n \"tone_id\": \"conscientiousness_big5\"\n },
\n {\n \"tone_name\": \"Extraversion\", \n
\"score\": 0.722, \n \"tone_id\": \"extraversion_big5\"\n
}, \n {\n \"tone_name\": \"Agreeableness\", \n
\"score\": 0.706, \n \"tone_id\": \"agreeableness_big5\"\n
}, \n {\n \"tone_name\": \"Emotional Range\", \n
\"score\": 0.974, \n \"tone_id\": \"neuroticism_big5\"\n
}\n ], \n \"category_name\": \"Social Tone\"\n }\n
]\n }\n}"
答案 0 :(得分:0)
基本上要做你想做的事情,你只需要在dict中导航直到你得到你的音调列表,然后循环每个音调,并为那个音调打印出它的tone_name
parsedJson = json.loads(jsonFile)
for tone in parsedJson['document_tone']['tone_categories'][0]['tones']:
print(tone['tone_name'])
# Anger
# Disgust
# Fear
# Joy
# Sadness
这是您的固定代码
import json
jsonText = None
with open('data.json','r') as f:
jsonText = f.read()
parsedJson = json.loads(jsonText)
for tone in parsedJson['document_tone']['tone_categories'][0]['tones']:
print(tone['tone_name'])