我在解析来自 IBM Watson 的 Tone Analyzer 的 JSON 响应时遇到问题。我知道我需要输入字典,然后遍历每个键,直到获得所需的信息。
当前代码
from __future__ import print_function
import json
import sys
from os.path import join, dirname
from watson_developer_cloud import ToneAnalyzerV3
from musixmatch import Musixmatch
musixmatch = Musixmatch('')
tone_analyzer = ToneAnalyzerV3(
username = "",
password = "",
version = '',
url = "https://gateway.watsonplatform.net/tone-analyzer/api" )
lyric = lyricwikia.get_lyrics('Dragonforce', 'Through the fire and flames')
fh = open('lyrics.txt', 'w+')
fh.write(lyric)
fh.close()
f = open('lyrics.txt', 'r+')
print("\ntone_chat() Dragonforce - Through the Fire and Flames:\n")
m = []
for line in f.readlines():
content_type = 'application/json'
tone = tone_analyzer.tone({"text": line}, content_type)
jsonText = json.dumps(tone, indent=2)
jsonParse = json.loads(jsonText)
m = jsonParse["document_tone"]["tones"]
print(m)
f.close()
我试图想办法从这个 JSON 响应中获取多个结果并将其作为密钥对存储在字典中,但我很难弄清楚如何去做。
电流输出
tone_chat() Dragonforce - 穿越烈火:
[]
[{'score': 0.511165, 'tone_id': 'sadness', 'tone_name': 'Sadness'}]
[{'score': 0.697052, 'tone_id': 'sadness', 'tone_name': 'Sadness'}]
[]
[{'score': 0.511165, 'tone_id': 'sadness', 'tone_name': 'Sadness'}]
[{'score': 0.697052, 'tone_id': 'sadness', 'tone_name': 'Sadness'}]
[{'score': 0.816009, 'tone_id': 'joy', 'tone_name': 'Joy'}, {'score':
0.822231, 'tone_id': 'tentative', 'tone_name': 'Tentative'}]
[]
[{'score': 0.518879, 'tone_id': 'sadness', 'tone_name': 'Sadness'}]
[{'score': 0.619897, 'tone_id': 'anger', 'tone_name': 'Anger'}]
[{'score': 0.608597, 'tone_id': 'anger', 'tone_name': 'Anger'}]
[{'score': 0.865077, 'tone_id': 'sadness', 'tone_name': 'Sadness'},
{'score': 0.724236, 'tone_id': 'analytical', 'tone_name': 'Analytical'}]
[]
[]
[]
[]
[{'score': 0.557844, 'tone_id': 'joy', 'tone_name': 'Joy'}, {'score':
0.898327, 'tone_id': 'confident', 'tone_name': 'Confident'}]
[]
[{'score': 0.695561, 'tone_id': 'sadness', 'tone_name': 'Sadness'},
{'score': 0.895415, 'tone_id': 'analytical', 'tone_name': 'Analytical'}]
[{'score': 0.616244, 'tone_id': 'sadness', 'tone_name': 'Sadness'},
{'score': 0.898327, 'tone_id': 'confident', 'tone_name': 'Confident'}]
[{'score': 0.859366, 'tone_id': 'sadness', 'tone_name': 'Sadness'}]
[]
[]
[{'score': 0.621329, 'tone_id': 'joy', 'tone_name': 'Joy'}]
[{'score': 0.743111, 'tone_id': 'joy', 'tone_name': 'Joy'}]
[]
[{'score': 0.612119, 'tone_id': 'joy', 'tone_name': 'Joy'}]
[]
[{'score': 0.524931, 'tone_id': 'joy', 'tone_name': 'Joy'}, {'score':
0.786991, 'tone_id': 'tentative', 'tone_name': 'Tentative'}]
[{'score': 0.92125, 'tone_id': 'confident', 'tone_name': 'Confident'}]
[]
[{'score': 0.8152, 'tone_id': 'analytical', 'tone_name': 'Analytical'},
{'score': 0.618451, 'tone_id': 'confident', 'tone_name': 'Confident'}]
[{'score': 0.638092, 'tone_id': 'sadness', 'tone_name': 'Sadness'},
{'score': 0.874372, 'tone_id': 'confident', 'tone_name': 'Confident'}]
[{'score': 0.707601, 'tone_id': 'sadness', 'tone_name': 'Sadness'},
{'score': 0.687768, 'tone_id': 'analytical', 'tone_name': 'Analytical'},
{'score': 0.618451, 'tone_id': 'confident', 'tone_name': 'Confident'}]
[{'score': 0.704642, 'tone_id': 'confident', 'tone_name': 'Confident'}]
[]
[{'score': 0.695561, 'tone_id': 'sadness', 'tone_name': 'Sadness'},
{'score': 0.895415, 'tone_id': 'analytical', 'tone_name': 'Analytical'}]
[{'score': 0.616244, 'tone_id': 'sadness', 'tone_name': 'Sadness'},
{'score': 0.898327, 'tone_id': 'confident', 'tone_name': 'Confident'}]
[{'score': 0.859366, 'tone_id': 'sadness', 'tone_name': 'Sadness'}]
[]
[]
[{'score': 0.516628, 'tone_id': 'fear', 'tone_name': 'Fear'}]
[{'score': 0.524235, 'tone_id': 'sadness', 'tone_name': 'Sadness'}]
[{'score': 0.52202, 'tone_id': 'sadness', 'tone_name': 'Sadness'}, {'score':
0.786991, 'tone_id': 'tentative', 'tone_name': 'Tentative'}]
[{'score': 0.961633, 'tone_id': 'confident', 'tone_name': 'Confident'}]
[]
[{'score': 0.695561, 'tone_id': 'sadness', 'tone_name': 'Sadness'},
{'score': 0.895415, 'tone_id': 'analytical', 'tone_name': 'Analytical'}]
[{'score': 0.616244, 'tone_id': 'sadness', 'tone_name': 'Sadness'},
{'score': 0.898327, 'tone_id': 'confident', 'tone_name': 'Confident'}]
[{'score': 0.859366, 'tone_id': 'sadness', 'tone_name': 'Sadness'}]
[][{'score': 0.816009, 'tone_id': 'joy', 'tone_name': 'Joy'}, {'score':
0.822231, 'tone_id': 'tentative', 'tone_name': 'Tentative'}]
[]
[{'score': 0.518879, 'tone_id': 'sadness', 'tone_name': 'Sadness'}]
[]
[{'score': 0.511165, 'tone_id': 'sadness', 'tone_name': 'Sadness'}]
[{'score': 0.697052, 'tone_id': 'sadness', 'tone_name': 'Sadness'}]
[{'score': 0.816009, 'tone_id': 'joy', 'tone_name': 'Joy'}, {'score':
0.822231, 'tone_id': 'tentative', 'tone_name': 'Tentative'}]
[]
我创建了一个名为“emotions”的字典,我试图将这 2 个值存储到其中,但它不起作用,所以我试图至少让这些值打印出来。我是否需要以不同的方式将音调名称和分数以密钥对的形式存储在字典中?感谢您的任何帮助!