Why am I seeing TypeError: string indices must be integers ?
item
is most likely a string in your code; the string indices are the ones in the square brackets, e.g., gravatar_id
. So I'd first check your data
variable to see what you received there; I guess that data
is a list of strings (or at least a list containing at least one string) while it should be a list of dictionaries.
A problem with dictionary (TypeError: string indices must be integers)
In this case you don't need a for loop if you have an array of dictionaries then you need to consider using for loop. Here you can simply do like below.
den = {
'first_name': 'den',
'last_name': 'elc',
'age': '16',
'city': 'slovakia',
}
print(f"\tFirst name:{den['first_name']}")
print(f"\tLast_name:{den['last_name']}")
print(f"\tAge:{den['age']}")
print(f"\tCity:{den['city']}")
TypeError: string indices must be integers (Python)
data['result']
is a dictionary. Iterating over dict
means iterating over its keys. Therefore key
variable stores a string. That's why key['id']
raises TypeError: string indices must be integers
.
Exception: TypeError(string indices must be integers)
You are trying to get an element from a JSON object, but passing a string.
The
data = json.dumps(bulk_item)
creates a string representing the object.
Try using bulk_item
on it's own.
Python string indices must be integers
It seems that what you get from the API is not exactly what you expect:
The variable fill
is a string (at least at the time you get the error).
As strings can't have string indices (like dictionaries can) you get the TypeError
exception.
To handle the exception and troubleshoot it, you can use try-except
, like so:
try:
price = fill['price']
except TypeError as e:
print(f"fill: {fill}, exception: {str(e)}")
This way, when there is an issue, the fill
value will be printed as well as the exception.
Python Flask TypeError: string indices must be integers
The error is caused by trying to slice a file object with []. This is not possible. You are probably trying to acces the pandas dataframe. You unfortunately forgot to add the correct variabel. So I would change
file['student']
To:
data['student']
TypeError: string indices must be integers; how can I fix this problem in my code?
The method used to iterate through the rows of the data frame is incorrect. So here
for row in dataset:
Only returns the 1st row , which usually contains all the column names which are normally strings. So when we do:text= row['Text']
It tries to extract the string at the index 'Text' and string indices can only be integers, hence the error.
eg: text= "abc"
>print(text[0]) #Output is 'a'.
>print(text['abc']) #Error - string indices must be integers
So the correct way to iterate through rows and extract the required column's value would be:
for index,row in df.iterrows():
text= row["Text"]
For information about the iterrows function , refer here : https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.iterrows.html
Related Topics
Split a String by Spaces -- Preserving Quoted Substrings -- in Python
Getting "Permission Denied" When Running Pip as Root on My MAC
Can You Add New Statements to Python's Syntax
What's the Bad Magic Number Error
Error Unicodedecodeerror: 'Utf-8' Codec Can't Decode Byte 0Xff in Position 0: Invalid Start Byte
How to Run Multiple While Loops at a Time in Pygame
Can Not Click on a Element: Elementclickinterceptedexception in Splinter/Selenium
How to Escape Os.System() Calls
Splitting a List into N Parts of Approximately Equal Length
How to Hide Console Window in Python
"Python" Not Recognized as a Command
Pythonic Way to Check If a List Is Sorted or Not
How to Draw Vertical Lines on a Given Plot