我对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”和最高分数。
^{pr2}$我的输出应该是:喜悦
我的代码是
^{3}$我的数据.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}"
基本上,要想在这里做你想做的事情,你只需要在dict中导航,直到你得到你的音调列表,然后循环每个音调,并为这个音调打印出它的
tone_name
这是你的固定密码
^{pr2}$相关问题 更多 >
编程相关推荐