简体   繁体   English

臭名昭著的TypeError:列表索引必须是整数,而不是str

[英]The infamous TypeError: list indices must be integers, not str

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. 我在下面有一个json响应,我试图只在motion_tone中打印色调。 The value of 'data' is a JSON response copied below “ data”的值是下面复制的JSON响应

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. 简而言之,我想从tone_category返回一个音调列表,以便可以写入一个csv文件。 Essentially my response minus the error 本质上我的回答减去了错误

here is the json response sample contained the variable data 这是包含变量data的json响应示例

{
"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. "category_id": "language_tone", "category_name": "Language Tone"在此行之前关闭该括号,它应该可以工作。

So it appears that the issue was associated with he way I was trying to loop thru the json in different "categories". 因此,似乎该问题与我试图通过不同“类别”中的json循环的方式有关。 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! 谢谢大家,我感谢您的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM