Python Forum
'str' object does not support item assignment - 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: 'str' object does not support item assignment (/thread-4180.html)



'str' object does not support item assignment - SolaVitae - Jul-28-2017

So currently i am experiencing an issue where i am trying to assign a value in a dictionary to an index value of a string


The code goes as follows


    searchValues = ['name', 'fame', 'game']
    values = {}

    versions = 5
    for v in range(1, versions+1):
         for value in searchValues:
             receivedValue = customFindFunction("{0}{1}".format(value, v), source)
             if (receivedValue is None):   #the find function returns None if it cant find the value specified
                 receivedValue = customFindfunction(value, source)
                 values[value] = receivedValue
                 continue
             valueKey = value + str(v)
             values[valueKey] = receivedValue  # ERROR OCCURS HERE, the receivedValue is "5"
The custom find function just searches a source string for a key (the value) and returns the actual value of the key (eg: string has Name : John, located in it, the function returns John, or None if it cant find the key)

Whenever the value is returned as None the first time, and it finds a value based on just a searchvalue, it enters into the values dict just fine

Whenever it finds the value on the first try and tries to enter a value into the values dict as values[value+v] it returns an error of "TypeError: 'str' object does not support item assignment"

This isnt the actual code, as the real code has quite a few other things going on during this that do not effect the code that is failing, so i left it out. The code functions fully and returns appropriate values when it finds a value based on its name eg: it finds "name" or "fame", but when it finds a value based on the value name plus a version number eg: "name1" or "fame1" it errors out


RE: 'str' object does not support item assignment - buran - Jul-28-2017

Please, check your actual code and ensure that you don't overwrite your dict values with some string (i.e. probably mistake where you typed values instead of value)