简体   繁体   English

TypeError:字符串索引必须是整数,而不是str //尝试获取key的值

[英]TypeError: string indices must be integers, not str // Trying to get value of key

I'm trying to get a key from a value that I am returned with, but when i use the easy approach to just get the value of a specific key I get the error: TypeError: string indices must be integers, not str 我试图从我返回的值中获取一个键,但是当我使用简单方法获取特定键的值时,我得到错误: TypeError:string indices必须是整数,而不是str

I also tried .get() method, but it did not work either. 我也尝试过.get()方法,但它也没有用。 Could someone please point me out what I am doing wrong? 有人可以指出我做错了什么吗?

>>> import urllib2
>>> url = 'http://192.168.250.1/ajax.app?SessionId=8ef05397-ef00-451a-bc1c-c0d61
5a4811d&service=getDp&plantItemId=1413'
>>> response = urllib2.urlopen(url)
>>> dict = response.read()
>>> dict
'{"service":"getDp","plantItemId":"1413","value":" 21.4","unit":"\xc2\xb0C"}'
>>> dict['value']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: string indices must be integers, not str

Looks like 'dict' is a variable of type string, not a dictionary. 看起来'dict'是字符串类型的变量,而不是字典。 You should parse the string into a suitable dictionary format (like JSON). 您应该将字符串解析为合适的字典格式(如JSON)。 Here's a code that will resolve that issue: 这是一个可以解决该问题的代码:

import json
json_string = response.read()
dict = json.loads(json_string)

Now, for dict['value'] you will get what you need. 现在,对于dict['value']你将得到你需要的东西。

response.read() is returning an object of type string and not a dictionary and you can index a string using only integer indices and hence you are getting your error. response.read()返回一个string类型的对象而不是字典,你可以只使用整数索引索引一个字符串,因此你得到了你的错误。

You need to parse this string and convert it to a dictionary. 您需要解析此字符串并将其转换为字典。 To convert a string of a dictionary back to a dictionary you can do this: 要将字典的字符串转换回字典,您可以执行以下操作:

import ast
dict = ast.literal_eval(dict)
print dict['value']

Tried it on my machine with Python 2.7 and it works. 用Python 2.7在我的机器上尝试它,它的工作原理。

暂无
暂无

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

相关问题 试图设置 cookie 但得到一个类型错误:字符串索引必须是整数,而不是 str - Trying to set cookies but get a TypeError: string indices must be integers, not str TypeError:尝试创建字典时,字符串索引必须是整数,而不是str - TypeError: string indices must be integers, not str , when trying to create a dictionary JSON值打印TypeError:字符串索引必须是整数,而不是str - Json value print TypeError: string indices must be integers, not str 当尝试从python字典中获取项时,“TypeError:字符串索引必须是整数,而不是str” - “TypeError: string indices must be integers, not str” when trying to get item from python dictionary typeerror字符串索引必须是整数,而不是str python - typeerror string indices must be integers not str python TypeError:字符串索引必须是整数,而不是str-OpenErp - TypeError: string indices must be integers, not str - OpenErp TypeError:字符串索引必须是整数,而不是python中的str - TypeError: string indices must be integers, not str in python Scrapy:TypeError:字符串索引必须是整数,而不是str? - Scrapy: TypeError: string indices must be integers, not str? zeromq:TypeError:字符串索引必须是整数,而不是str - zeromq: TypeError: string indices must be integers, not str TypeError:字符串索引必须是整数,而不是Pt []中的str - TypeError: string indices must be integers, not str in Pt[]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM