Python Forum
TypeError: string indices must be integers
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError: string indices must be integers
#1
Hello All,

When I run the below script, I get this error...

print ('{}|{}|{}'.format(x['hostname'], x ['vlanid'], x['vlandescription']))
TypeError: string indices must be integers

...here is my script...
import sys
import requests,urllib3,json,pprint
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

def get_vlanid ():
    response = raw_input("Please Enter VLAN ID:")
    url = 'https://<URL>:8443/nds/vlanDB/vlantool/vlanid/'
    body = '[{"vlanid": "%s"}]' % response
    s = requests.session()
    req = s.post(url, headers={'apikey': '<API_KEY>','Accept': 'application/json', 'Content-type': 'application/json'}, verify=False, data=body)
    result = json.loads(req.content)
    for x in result:
        print ('{}|{}|{}'.format(x['hostname'], x ['vlanid'], x['vlandescription']))

#get_all()
get_vlanid ()
I think on the json I'm pulling from is identifying the vlanid, as an integer? And, I need to tell the print command to expect such?

Any advice?
Reply
#2
When you loop over result you get dictionary key string back.
result is just a Python dictionary,that json.load() make.
There also no need to use json.load() as Requests has this build in.
Make your error and fix.
>>> import requests
>>> 
>>> r = requests.get('https://api.github.com/feeds')
>>> data = r.json()
>>> data
{'_links': {'timeline': {'href': 'https://github.com/timeline',
                         'type': 'application/atom+xml'},
            'user': {'href': 'https://github.com/{user}',
                     'type': 'application/atom+xml'}},
 'timeline_url': 'https://github.com/timeline',
 'user_url': 'https://github.com/{user}'}
>>> 
>>> for x in data:
...     x['timeline_url']
...     
Traceback (most recent call last):
  File "<string>", line 428, in runcode
  File "<interactive input>", line 2, in <module>
TypeError: string indices must be integers

>>> # x is just key strings 
>>> # To get timeline_url
>>> data['timeline_url']
'https://github.com/timeline'
>>> 
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  tuple indices must be integers or slices, not str cybertooth 16 11,079 Nov-02-2023, 01:20 PM
Last Post: brewer32
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,091 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  boto3 - Error - TypeError: string indices must be integers kpatil 7 1,184 Jun-09-2023, 06:56 PM
Last Post: kpatil
  Response.json list indices must be integers or slices, not str [SOLVED] AlphaInc 4 6,185 Mar-24-2023, 08:34 AM
Last Post: fullytotal
  "TypeError: string indices must be integers, not 'str'" while not using any indices bul1t 2 1,931 Feb-11-2023, 07:03 PM
Last Post: deanhystad
  Error "list indices must be integers or slices, not str" dee 2 1,394 Dec-30-2022, 05:38 PM
Last Post: dee
  TypeError: string indices must be integers JonWayn 12 3,261 Aug-31-2022, 03:29 PM
Last Post: deanhystad
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,763 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  TypeError: list indices must be integers or slices, not range Anldra12 2 2,501 Apr-22-2022, 10:56 AM
Last Post: Anldra12
  string indices must be integers when parsing Json ilknurg 3 6,205 Mar-10-2022, 11:02 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020