Python Forum
TypeError: string indices must be integers
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError: string indices must be integers
#1
Am learning to create a python API. I want to get a return a student object whenever there's a match of student name when I loop through the students data.

Please tell me what I am doing wrong. I get the error TypeError: string indices must be integers

class Student(Resource):
      #get student by name
    def get(self, name):
        for student in students:
            if name == student["name"]:
                 return student, 200
        return "student not found", 404
Reply
#2
name = '404' would be a string, name = 404 an integer.
It's requesting an integer, not string.
when in doubt, you can find out what the type is with print(type(name))
Reply
#3
I may have left out some important info. I am using firebase realtime db.
This solved my problem. I was supposed to loop through by means of key,value.

class Student(Resource):
    dbStudents = db.reference("/students")
    students = dbStudents.get()

    def get(self, name):
        for key, value in students.items():
            if name == value["name"]:
                return value, 200
        return "student not found", 404
Reply
#4
it looks you are getting a dict, so you can do
for student in students.values():
    if name == student["name"]:
        return student, 200
Now, the question is why you don't filter the result set from the db in the first place?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
@burah,
that works fine too. Smile
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  tuple indices must be integers or slices, not str cybertooth 16 11,884 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,265 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  boto3 - Error - TypeError: string indices must be integers kpatil 7 1,342 Jun-09-2023, 06:56 PM
Last Post: kpatil
  Response.json list indices must be integers or slices, not str [SOLVED] AlphaInc 4 6,532 Mar-24-2023, 08:34 AM
Last Post: fullytotal
  "TypeError: string indices must be integers, not 'str'" while not using any indices bul1t 2 2,090 Feb-11-2023, 07:03 PM
Last Post: deanhystad
  Error "list indices must be integers or slices, not str" dee 2 1,509 Dec-30-2022, 05:38 PM
Last Post: dee
  TypeError: string indices must be integers JonWayn 12 3,532 Aug-31-2022, 03:29 PM
Last Post: deanhystad
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,951 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  TypeError: list indices must be integers or slices, not range Anldra12 2 2,653 Apr-22-2022, 10:56 AM
Last Post: Anldra12
  string indices must be integers when parsing Json ilknurg 3 6,478 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