Skip to content
← Back to Community
TypeError: string indices must be integers | Discord.py
Profile icon
DwellerIgor

So I have a line in main.py levelling = cluster["discord"]["levelling"], but i think i need to use numbers instead of 'discord' and 'levelling': levelling = cluster[0][1]. But i dont understand which numbers do i put in there. Thank you!

Voters
Profile icon
DwellerIgor
Comments
hotnewtop
Profile icon
markfilan

TypeError: means that you are trying to perform an operation on a value whose type is not compatible with that operation. An Iterable is a collection of elements that can be accessed sequentially . In Python, iterable objects are indexed using numbers . When you try to access an iterable object using a string or a float as the index, an error will be returned as TypeError: string indices must be integers. This means that when you're accessing an iterable object like a string or float value, you must do it using an integer value.

For example, str[hello"] and str[2.1] as indexes. As these are not integers, a TypeError exception is raised. This means that when you’re accessing an iterable object like a string or float value, you must do it using an integer value . If you are accessing items from a dictionary , make sure that you are accessing the dictionary itself and not a key in the dictionary.

Python supports slice notation for any sequential data type like lists, strings , tuples, bytes, bytearrays, and ranges. When working with strings and slice notation, it can happen that a TypeError: string indices must be integers is raised, pointing out that the indices must be integers, even if they obviously are.

Profile icon
VulcanWM

cluster is a list and you need to enter the index of the db in the list cluster and the index of collection in the list of the db

Profile icon
DwellerIgor

but what should i put instead of levelling = cluster["discord"]["levelling"]? Sorry, im really confused @VulcanWM

Profile icon
VulcanWM

so do:

clusterindex = cluster.index("discord"} thedb = cluster[clusterindex] dbindex = thedb.index("levelling") levelling = thedb[dbindex]

@DwellerIgor

Profile icon
DwellerIgor

ValueError: substring not found @VulcanWM

Profile icon
DwellerIgor

Traceback (most recent call last):
File "main.py", line 5, in
import levelsys
File "/home/runner/THANOS-LEVELSYS/levelsys.py", line 17, in
clusterindex = cluster.index("discord")
ValueError: substring not found

Profile icon
VulcanWM

i dont get what you are trying to do. are you trying to use mongodb? @DwellerIgor

Profile icon
VulcanWM

If you are trying to use mongodb, then do:

import pymongo cluster = pymongo.MongoClient(os.getenv("cluster")) levelling = cluster["discord"]["levelling"]

@DwellerIgor

Profile icon
DwellerIgor

well, i fixed that, but i get this again pymongo.errors.ConfigurationError: A DNS label is empty. @VulcanWM