Reply

trouble getting passed the string indices error in python script

Member
Posts: 8
3362     0

I need a hot poker in my eye! Sometimes this code works fine. Sometimes I get this error: 

 

Traceback (most recent call last):
File "E:\SNtoInfoblox\uploads\test.py", line 16, in <module>
response = requests.request("DELETE", urld + ref['_ref'], auth=(id,pw),verify=False)
TypeError: string indices must be integers

 

import requests
import json

url = 'https://infoblox.com/wapi/v2.3.1/macfilteraddress?filter=SN_Test'
urld = 'https://infoblox.com/wapi/v2.3.1/'
id = 'admin'
pw = 'infoblox'

r = requests.get(url, auth=(id, pw), verify=False)

json_input = r.text
decoded = json.loads(json_input)

for ref in decoded:
   response = requests.request("DELETE", urld + ref['_ref'], auth=(id,pw),verify=False)

Anyone able to see the problem, prettty please??

Re: trouble getting passed the string indices error in python script

Authority
Posts: 18
3363     0

Hello,

 

The error returned means that the value of  ref, in the for loop used, is not recognized/found as an index for the array of data inside the variable decoded.

 

This is expected to happen when the data, in decoded, is of dictionary type, rather than list type.

 

To ensure that the json content returned over the GET request is alway of list type, add and set the _return_as_object argument to 0 (zero). The below modification should fix the error:

 

import requests
import json

url = 'https://infoblox.com/wapi/v2.3.1/macfilteraddress?filter=SN_Test&_return_as_object=0'
urld = 'https://infoblox.com/wapi/v2.3.1/'
id = 'admin'
pw = 'infoblox'

r = requests.get(url, auth=(id, pw), verify=False)

json_input = r.text
decoded = json.loads(json_input)

for ref in decoded:
   response = requests.request("DELETE", urld + ref['_ref'], auth=(id,pw),verify=False)

Hope this helps.

 

Regards

Re: trouble getting passed the string indices error in python script

New Member
Posts: 2
3363     0

TypeError: means that you are trying to perform an operation on a value whose type is not compatible with that operation. An Iterable is a collection of elements that can be accessed sequentially . In Python, iterable objects are indexed using numbers . When you try to access an iterable object using a string or a float as the index, an error will be returned as TypeError: string indices must be integers. This means that when you're accessing an iterable object like a string or float value, you must do it using an integer value.

 

Showing results for 
Search instead for 
Did you mean: 

Recommended for You