臭名昭着的TypeError:list索引必须是整数,而不是str

时间:2017-03-11 22:01:51

标签: python json

过去几天我一直在寻找,虽然我对错误有了更好的理解,但我仍然没有完整的解决方案。我有一个json响应下面,我试图打印emotion_tone中的音调。 “data”的值是复制在

下面的JSON响应

我的循环:

for key in data.keys():
doc_tone = data[key]['tone_categories'][0]['tones']
for tones in doc_tone:
    print (tones)

上面的代码返回以下内容

{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

对于我的生活,我无法弄清楚如何修复错误。我搜索了一堆,但我可以搞清楚。我知道错误是由一个列表引起的,当一个索引被预期时,有人可以帮忙,这让我抓狂!

简而言之,我想从tone_category返回一个音调列表,这样我就可以写一个csv文件了。基本上我的回答减去错误

这里是包含变量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"
  }

我感谢你们提供的任何帮助。 感谢

2 个答案:

答案 0 :(得分:1)

格式化时出错。你打开了两个[但只关闭了一个,这使循环尝试对此应用操作:

"category_id": "language_tone", "category_name": "Language Tone" 在这一行之前关闭那个括号,它应该可以工作。

答案 1 :(得分:0)

所以看起来这个问题与我试图通过json在不同的&#34;类别&#34;中循环的方式有关。一旦该类别被指定,我就能够毫无问题地返回数据。代码如下:

Weather now in Warsaw, pl 

Clouds: 20 %
Rain: 15 %
Wind speed: 2.6
Wind degree: 340
Humidity: 75 %
Temperature: 5.0 celsius
Max temperature: 5.0 celsius
Min temperature: 5.0 celsius
Weather status: few clouds

结果如下:

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)

谢谢你们,感谢你的帮助!