I have been searching for the past few days and although I have a better understanding of the error, I still don't have a full solution. I have a json responce below, that I am trying to print just the tones in emotion_tone. The value of 'data' is a JSON response copied below
My loop:
for key in data.keys():
doc_tone = data[key]['tone_categories'][0]['tones']
for tones in doc_tone:
print (tones)
The code above returns the following
{u'tone_name': u'Anger', u'score': 0.105542, u'tone_id': u'anger'}
{u'tone_name': u'Disgust', u'score': 0.134394, u'tone_id': u'disgust'}
{u'tone_name': u'Fear', u'score': 0.150116, u'tone_id': u'fear'}
{u'tone_name': u'Joy', u'score': 0.083824, u'tone_id': u'joy'}
{u'tone_name': u'Sadness', u'score': 0.605555, u'tone_id': u'sadness'}
Traceback (most recent call last):
File "Untitled 5.py", line 21, in <module>
doc_tone = data[key]['tone_categories'][0]['tones']
TypeError: list indices must be integers, not str
For the life of me I can't figure out how to fix the error. I have search a bunch but I can figure it out. I know the error is caused by a list when a index is expected, can someone please help, this is driving me crazy!
In short I would like to return a list of tones, from a tone_category, so I can write to a csv file. Essentially my response minus the error
here is the json response sample contained the variable data
{
"document_tone": {
"tone_categories": [
{
"tones": [
{
"score": 0.105542,
"tone_id": "anger",
"tone_name": "Anger"
},
{
"score": 0.134394,
"tone_id": "disgust",
"tone_name": "Disgust"
},
{
"score": 0.150116,
"tone_id": "fear",
"tone_name": "Fear"
},
{
"score": 0.083824,
"tone_id": "joy",
"tone_name": "Joy"
},
{
"score": 0.605555,
"tone_id": "sadness",
"tone_name": "Sadness"
}
],
"category_id": "emotion_tone",
"category_name": "Emotion Tone"
},
{
"tones": [
{
"score": 0,
"tone_id": "analytical",
"tone_name": "Analytical"
},
{
"score": 0,
"tone_id": "confident",
"tone_name": "Confident"
},
{
"score": 0.966403,
"tone_id": "tentative",
"tone_name": "Tentative"
}
],
"category_id": "language_tone",
"category_name": "Language Tone"
},
{
"tones": [
{
"score": 0.915827,
"tone_id": "openness_big5",
"tone_name": "Openness"
},
{
"score": 0.064387,
"tone_id": "conscientiousness_big5",
"tone_name": "Conscientiousness"
},
{
"score": 0.375757,
"tone_id": "extraversion_big5",
"tone_name": "Extraversion"
},
{
"score": 0.579473,
"tone_id": "agreeableness_big5",
"tone_name": "Agreeableness"
},
{
"score": 0.287825,
"tone_id": "emotional_range_big5",
"tone_name": "Emotional Range"
}
],
"category_id": "social_tone",
"category_name": "Social Tone"
}
I appreciate any help you guys can provide. Thanks
There is a mistake in formating. You opened two [
but only closed one, which made the loop try to apply the operation on this:
"category_id": "language_tone", "category_name": "Language Tone"
Close that bracket before this lines,and it should work.
So it appears that the issue was associated with he way I was trying to loop thru the json in different "categories". Once that category was specifies I was able to return the data without any issues. code below:
for key in data:
if key == 'document_tone':
for tones in data['document_tone']:
doc_tone = data[key]['tone_categories'][0]['tones']
for tones in doc_tone:
print (tones)
The results below:
{u'tone_name': u'Anger', u'score': 0.089058, u'tone_id': u'anger'}
{u'tone_name': u'Disgust', u'score': 0.050212, u'tone_id': u'disgust'}
{u'tone_name': u'Fear', u'score': 0.607714, u'tone_id': u'fear'}
{u'tone_name': u'Joy', u'score': 0.17284, u'tone_id': u'joy'}
{u'tone_name': u'Sadness', u'score': 0.142486, u'tone_id': u'sadness'}
Thanks guys I appreciate your help!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.