Python Forum
string indices must be integers
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
string indices must be integers
#1
So, I have:

data = json.loads(requests.get(url).text)
print(type(data))
print(data[0])

<class 'list'>
{'sentence': ['what', 'is', 'a', 'transistor'],
 'tags': ['WH', 'AUX', 'DT', 'NN']}
then I want to separate 'sentence' and 'tags'

sents = []
tags = []

for element in data:
  sents.append(element['sentence'])
  tags.append(element['tags'])
and here a problem:

TypeError                                 Traceback (most recent call last)
<ipython-input-8-68927e1d38ae> in <module>()
      3 
      4 for element in data:
----> 5   sents.append(element['sentence'])
      6   tags.append(element['tags'])

TypeError: string indices must be integers
what is wrong?

P.S. if I just print it - it is printed without problems.
Reply
#2
try print(type(data[0])). It looks like elements are strings not dict or to be precise - dict stored in list as string

also
data = json.loads(requests.get(url).text)
is same as
data = requests.get(url).json()
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
#3
No, actually it is a dict

print(type(data[0]))
<class 'dict'>
Reply
#4
That is really weird. Try to print element. Is it possible that data has different types of elements,e.g. data[0] is dict, but are all elements dict?

print(set(type(element) for element in data))
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
data is the dictionary. When you do for element in data, element takes on the value of the key, first iteration, it is 'sentence', second iteration, it is 'tags' Get rid of the for loop and it will work.
import json
data = json.loads('{"sentence": ["what", "is", "a", "transistor"],"tags": ["WH", "AUX", "DT", "NN"]}')
print(type(data))
print(data["sentence"])
 
sents = []
tags = []
 

sents.append(data['sentence'])
tags.append(data['tags'])
Reply
#6
Ok, I get it. There are a few strings in a list. But it is interesting, why I can print it without any problems.

like this:
for element in data:
  print(element['sentence'])
  print(element['tags'])

So, the moral of the story: check carefully your dataset before work with it!
Reply
#7
(Apr-22-2020, 10:14 AM)constantin01 Wrote: There are a few strings in a list. But it is interesting, why I can print it without any problems.
Because if element is a string, you cannot access it like with dict key, you can use int indexes though:

>>> element = 'some string'
>>> element['sentencies']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: string indices must be integers
>>> element[0]
's'
>>> element[3:]
'e string'
>>>

(Apr-22-2020, 10:13 AM)TomToad Wrote: data is the dictionary.
Nope, data is list, but as it turns out elements are of mixed type - dicts and strings
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


Possibly Related Threads…
Thread Author Replies Views Last Post
  tuple indices must be integers or slices, not str cybertooth 16 11,077 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,181 Jun-09-2023, 06:56 PM
Last Post: kpatil
  Response.json list indices must be integers or slices, not str [SOLVED] AlphaInc 4 6,182 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,393 Dec-30-2022, 05:38 PM
Last Post: dee
  TypeError: string indices must be integers JonWayn 12 3,259 Aug-31-2022, 03:29 PM
Last Post: deanhystad
  TypeError: list indices must be integers or slices, not range Anldra12 2 2,500 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
  TypeError: string indices must be integers hendern 2 2,957 Oct-02-2020, 10:16 PM
Last Post: hendern

Forum Jump:

User Panel Messages

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