[英]I am getting python error TypeError: String indices must be integers, not str
Below is the dictionary that I get from a JSON response 以下是我从JSON响应中获得的字典
{u'definitions': [{u'text': u'One venerated for experience, judgment, and wisdom.', u'attribution': u'from The American Heritage\xae Dictionary of the English Language, 4th Edition'}, {u'text': u'Having or exhibiting wisdom and calm judgment.', u'attribution': u'from The American Heritage\xae Dictionary of the English Language, 4th Edition'}, {u'text': u'Proceeding from or marked by wisdom and calm judgment: sage advice. ', u'attribution': u'from The American Heritage\xae Dictionary of the English Language, 4th Edition'}, {u'text': u'Archaic Serious; solemn.', u'attribution': u'from The American Heritage\xae Dictionary of the English Language, 4th Edition'}, {u'text': u'Any of various plants of the genus Salvia, especially S. officinalis, having aromatic grayish-green, opposite leaves. Also called ramona.', u'attribution': u'from The American Heritage\xae Dictionary of the English Language, 4th Edition'}, {u'text': u'The leaves of this plant used as a seasoning.', u'attribution': u'from The American Heritage\xae Dictionary of the English Language, 4th Edition'}, {u'text': u'Any of various similar or related plants in the mint family.', u'attribution': u'from The American Heritage\xae Dictionary of the English Language, 4th Edition'}, {u'text': u'Sagebrush.', u'attribution': u'from The American Heritage\xae Dictionary of the English Language, 4th Edition'}]}
I am trying to access the first two 'text' by using this code statement: 我正在尝试使用此代码语句访问前两个“文本”:
text1 = [body["definitions"][0]["text"],
body["definitions"][1]["text"]]
I get the strings that I need. 我得到了我需要的琴弦。
But the actual problem is that when I run the same code on my raspberry Pi 2, I get the following error 但是实际的问题是,当我在树莓派2上运行相同的代码时,出现以下错误
TypeError: string indices must be integers, not str TypeError:字符串索引必须是整数,而不是str
Any help would be appreciated. 任何帮助,将不胜感激。
body["definitions"]
is returning a string, say "hello", so if you write: body["definitions"]
返回一个字符串,说“ hello”,因此,如果您编写:
body["definitions"]["0"]
that is equivalent to: 等效于:
"hello"["0"]
which produces the error: 产生错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: string indices must be integers
But if you write: 但是,如果您写:
"hello"[0]
you get: 你得到:
'h'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.