![]() |
TypeError: string indices must be integers - 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: TypeError: string indices must be integers (/thread-30058.html) |
TypeError: string indices must be integers - hendern - Oct-02-2020 Hi! I have a piece of code that works fine: result_ack = client.service.AcknowledgeResults([{'_value_1': 'OK', 'PassageID': id}])But when I add the hard coded value to a variable and then try to pass it to: variable = "[{'_value_1': 'OK', 'PassageID': " + id + " }]" result_ack = client.service.AcknowledgeResults(variable)I receive "TypeError: string indices must be integers" Any help much appreciated:-) BR, Henrik
RE: TypeError: string indices must be integers - bowlofred - Oct-02-2020 In the first line you are constructing a dict (with values) inside a list. In the second line you are constructing a string with brackets that looks like the first (but is still just a string). If you want to hold it as a temporary object, it would be similar to below. Just take the stuff inside the parenthesis and assign to a variable. variable = [{'_value_1': 'OK', 'PassageID': id }] result_ack = client.service.AcknowledgeResults(variable)Can you explain more why you think that creating a string is useful for you. Also, it looks like you're using a variable named id . That will shadow the existing function id() . Using a different name would be recommended.>>> id(3) 4479778544 >>> id = "myid" >>> id(3) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'str' object is not callable RE: TypeError: string indices must be integers - hendern - Oct-02-2020 Thanks, bowlofred, for pointing me in the right direction! And micseydel for fixing my post:-) I changed the variable to list and appended the dict element(s): variable = [] variable.append({'_value_1': 'OK', 'PassageID': PassageID }) result_ack = client.service.AcknowledgeResults(variable)Then it worked as intended. I do not use "id" as variable name, just made the code a bit to anonymous;-) Thanks again, Henrik |