Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

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

I also tried .get() method, but it did not work either. 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
like image 823
user3053452 Avatar asked Mar 30 '16 08:03

user3053452


People also ask

How do you convert string to int in Python?

To convert a string to integer in Python, use the int() function. This function takes two parameters: the initial string and the optional base to represent the data. Use the syntax print(int("STR")) to return the str as an int , or integer.

How do you resolve list indices must be integers or slices not str?

The Python "TypeError: list indices must be integers or slices, not str" occurs when we use a string instead of an integer to access a list at a specific index. To solve the error, use the int() class to convert the string to an integer, e.g. my_list[int(my_str)] .

What does type error string indices must be integers mean?

The Python "TypeError: string indices must be integers" occurs when we use a non-integer value to access a string at an index. To solve the error, make sure to use an integer, e.g. my_str[2] or a slice, e.g. my_str[0:3] when accessing a string at a specific index.

How do I fix TypeError string indices must be integers?

If you encounter this error message, double check to make sure you are using the numerical index value to access elements instead of a string value.


2 Answers

Looks like 'dict' is a variable of type string, not a dictionary. You should parse the string into a suitable dictionary format (like 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.

like image 157
Belphegor Avatar answered Oct 04 '22 01:10

Belphegor


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.

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.

like image 42
Banach Tarski Avatar answered Oct 04 '22 00:10

Banach Tarski