Indeksy łańcuchowe muszą być liczbami całkowitymi, a nie łańcuchowymi, pobierającymi wartość ze słownika
Próbuję pobrać wartość ze słownika. Działa dobrze w jednej części mojego kodu, ale następnie przerywa w następnej.
if user is not None:
user["Username"] = "Temp-"+user["Username"]
print "USER ID: " + str(user["ID"]) #Works fine
#Rename user in source account
patch_user(user, headers)
Użytkownik łatki:
def patch_user( user, headers ):
"""Patch a User field based on their ID
"""
patched_user = request('patch_user', user["AccountID"], user, headers)
Następnie na żądanie:
def request( request, account_id, user, headers):
"""Send a User request to the server
Send a request to the server
"""
url = api_root + "/accounts/" + str(account_id)
function_dictionary = {'get_user_from_id': (requests.get, #Function
(url + "/users/" + str(user),), #Tuple of Arguments
{'headers': headers}), #Dictionary of keyword args
'patch_user':(requests.patch,
(url + "/users/" + str(user["ID"]),),
{'headers': headers, 'data':json.dumps(user)})
func, args, kwargs = function_dictionary[request]
result = func(*args, **kwargs)
#Throw exception if non-200 response
result.raise_for_status()
#Result from query
print "Query " + request + " result: " + result.text
return result
Otrzymuję następujący błąd:
TypeError: string indices must be integers, not str
w(url + "/users/" + str(user["ID"]),),
Każda pomoc jest doceniana!
Edytować Pełny ślad:
Traceback (most recent call last):
File "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\Extensio
ns\Microsoft\Python Tools for Visual Studio\2.0\visualstudio_py_util.py", line 7
6, in exec_file
exec(code_obj, global_variables)
File "C:\Front-End\pcli-front
.py", line 179, in <module>
user_operations.move_user(arguments['<source>'],arguments['<destination>'],
arguments['<id>'], headers)
File "C:\Front-End\user_opera
tions.py", line 27, in move_user
user = get_user_from_id(source_account, user_id, headers)
File "C:\Front-End\user_opera
tions.py", line 68, in get_user_from_id
user = request('get_user_from_id', account_id, user_id, headers)
File "C:\Front-End\user_opera
tions.py", line 131, in request
(url + "/users/" + str(user["ID"]),),
TypeError: string indices must be integers, not str