Python Forum
Getting TypeError: list indices must be integers or slices not str
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting TypeError: list indices must be integers or slices not str
#11
To solve the issue Im doing like below, verifying if the body is not empty, but its not working it appears the same issue.

def song_lyric(song_name,artist_name):
    while True:
        querystring = apiurl_musixmatch + "matcher.lyrics.get?q_track=" + urllib.parse.quote(song_name) + "&q_artist=" + urllib.parse.quote(artist_name) +"&apikey=" + apikey_musixmatch + "&format=json&f_has_lyrics=1"
        #matcher.lyrics.get?q_track=sexy%20and%20i%20know%20it&q_artist=lmfao
        request = urllib.request.Request(querystring)
        #request.add_header("Authorization", "Bearer " + client_access_token)
        request.add_header("User-Agent", "curl/7.9.8 (i686-pc-linux-gnu) libcurl 7.9.8 (OpenSSL 0.9.6b) (ipv6 enabled)") #Must include user agent of some sort, otherwise 403 returned
        while True:
            try:
                response = urllib.request.urlopen(request, timeout=4) #timeout set to 4 seconds; automatically retries if times out
                raw = response.read()
                #print(raw)
            except socket.timeout:
                print("Timeout raised and caught")
                continue
            break
        json_obj = json.loads(raw.decode('utf-8'))
        if json_obj["message"]["body"] != "": # verifying if body is empty
            print(json_obj)
            body = json_obj["message"]["body"]["lyrics"]["lyrics_body"]
            copyright = json_obj["message"]["body"]["lyrics"]["lyrics_copyright"]
            tracking_url = json_obj["message"]["body"]["lyrics"]["html_tracking_url"]
            if(tracking_url!= ""):
                #print(body) #this print blank
                #print(tracking_url)
                lyrics_tracking(tracking_url)
                return (body + "\n\n" +copyright)
            else:
                return "None"
        else:
            return "None"
Also when there is no track the response is:

{'message': {'header': {'status_code': 404, 'execute_time': 0.0011160373687744}, 'body': []}}

Thanks! It worked with "if json_obj["message"]["body"] != [].
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  tuple indices must be integers or slices, not str cybertooth 16 11,455 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,157 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  boto3 - Error - TypeError: string indices must be integers kpatil 7 1,231 Jun-09-2023, 06:56 PM
Last Post: kpatil
  Why do I have to repeat items in list slices in order to make this work? Pythonica 7 1,321 May-22-2023, 10:39 PM
Last Post: ICanIBB
  Response.json list indices must be integers or slices, not str [SOLVED] AlphaInc 4 6,368 Mar-24-2023, 08:34 AM
Last Post: fullytotal
Question How to append integers from file to list? Milan 8 1,444 Mar-11-2023, 10:59 PM
Last Post: DeaD_EyE
  "TypeError: string indices must be integers, not 'str'" while not using any indices bul1t 2 2,012 Feb-11-2023, 07:03 PM
Last Post: deanhystad
  Error "list indices must be integers or slices, not str" dee 2 1,455 Dec-30-2022, 05:38 PM
Last Post: dee
  TypeError: string indices must be integers JonWayn 12 3,383 Aug-31-2022, 03:29 PM
Last Post: deanhystad
  read a text file, find all integers, append to list oldtrafford 12 3,517 Aug-11-2022, 08:23 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

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