Python Forum

Full Version: string indices must be integers
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
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()
No, actually it is a dict

print(type(data[0]))
<class 'dict'>
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))
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'])
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!
(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