简体   繁体   English

我收到python错误TypeError:字符串索引必须是整数,而不是str

[英]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.

相关问题 我为什么会收到此错误TypeError:列表索引必须是整数或切片,而不是str - Why am I getting this error TypeError: list indices must be integers or slices, not str typeerror字符串索引必须是整数,而不是str python - typeerror string indices must be integers not str python TypeError:字符串索引必须是整数,而不是python中的str - TypeError: string indices must be integers, not str in python Python和网络X中的错误-TypeError:字符串索引必须是整数,而不是str - Error in Python and network X - TypeError: string indices must be integers, not str 为什么会出现TypeError:字符串索引必须为整数? - Why am I getting TypeError: string indices must be integers? 为什么我会收到这个 TypeError:字符串索引必须是整数 - why am i getting this TypeError: string indices must be integers 为什么我在通过 for 循环从另一个附加新列表时收到此错误? 类型错误:列表索引必须是整数或切片,而不是 str - Why am I getting this error while appending a new list from another by for loop ? TypeError: list indices must be integers or slices, not str 我收到此错误-&gt; TypeError:字符串索引必须为整数 - I'm getting this error --> TypeError: string indices must be integers python 错误 - TypeError:字符串索引必须是整数 - python error - TypeError: string indices must be integers Python错误:“ TypeError:字符串索引必须为整数” - Python Error:“TypeError: string indices must be integers”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM