Python Forum

Full Version: TypeError: string indices must be integers
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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

Error:
Complete error: result_ack = client.service.AcknowledgeResults(id) File "/usr/lib/python3/dist-packages/zeep/client.py", line 45, in __call__ self._op_name, args, kwargs) File "/usr/lib/python3/dist-packages/zeep/wsdl/bindings/soap.py", line 110, in send options=options) File "/usr/lib/python3/dist-packages/zeep/wsdl/bindings/soap.py", line 68, in _create serialized = operation_obj.create(*args, **kwargs) File "/usr/lib/python3/dist-packages/zeep/wsdl/definitions.py", line 197, in create return self.input.serialize(*args, **kwargs) File "/usr/lib/python3/dist-packages/zeep/wsdl/messages/soap.py", line 64, in serialize self.body.render(body, body_value) File "/usr/lib/python3/dist-packages/zeep/xsd/elements/element.py", line 191, in render self._render_value_item(parent, value, render_path) File "/usr/lib/python3/dist-packages/zeep/xsd/elements/element.py", line 215, in _render_value_item return self.type.render(node, value, None, render_path) File "/usr/lib/python3/dist-packages/zeep/xsd/types/complex.py", line 253, in render element.render(parent, element_value, child_path) File "/usr/lib/python3/dist-packages/zeep/xsd/elements/indicators.py", line 241, in render element.render(parent, element_value, child_path) File "/usr/lib/python3/dist-packages/zeep/xsd/elements/element.py", line 191, in render self._render_value_item(parent, value, render_path) File "/usr/lib/python3/dist-packages/zeep/xsd/elements/element.py", line 215, in _render_value_item return self.type.render(node, value, None, render_path) File "/usr/lib/python3/dist-packages/zeep/xsd/types/complex.py", line 224, in render attr_value = value[name] if name in value else NotSet TypeError: string indices must be integers
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
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