- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
trouble getting passed the string indices error in python script
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
07-05-2018 09:01 AM
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
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
07-20-2018 11:32 AM
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
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
01-03-2021 10:18 PM
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.