Python Forum
string indices must be integers - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: string indices must be integers (/thread-26142.html)



string indices must be integers - constantin01 - Apr-22-2020

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.


RE: string indices must be integers - buran - Apr-22-2020

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()



RE: string indices must be integers - constantin01 - Apr-22-2020

No, actually it is a dict

print(type(data[0]))
<class 'dict'>



RE: string indices must be integers - buran - Apr-22-2020

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))



RE: string indices must be integers - TomToad - Apr-22-2020

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'])



RE: string indices must be integers - constantin01 - Apr-22-2020

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!


RE: string indices must be integers - buran - Apr-22-2020

(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