私はPythonにはとても新しいです。私はそれを学んでいます。jsonから値を抽出し、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}"
輸入JSON json_data =オープン( 'data.json') データ= json.load(json_data) #print(データ) =データ[ "tone_categories"] [0] [ "トーン"] プリント私が試したコードは – JT28