Python Forum
TypeError: string indices must be integers
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError: string indices must be integers
#1
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
Reply
#2
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
Reply
#3
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  tuple indices must be integers or slices, not str cybertooth 16 11,069 Nov-02-2023, 01:20 PM
Last Post: brewer32
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,091 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  boto3 - Error - TypeError: string indices must be integers kpatil 7 1,181 Jun-09-2023, 06:56 PM
Last Post: kpatil
  Response.json list indices must be integers or slices, not str [SOLVED] AlphaInc 4 6,178 Mar-24-2023, 08:34 AM
Last Post: fullytotal
  "TypeError: string indices must be integers, not 'str'" while not using any indices bul1t 2 1,927 Feb-11-2023, 07:03 PM
Last Post: deanhystad
  Error "list indices must be integers or slices, not str" dee 2 1,393 Dec-30-2022, 05:38 PM
Last Post: dee
  TypeError: string indices must be integers JonWayn 12 3,256 Aug-31-2022, 03:29 PM
Last Post: deanhystad
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,755 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  TypeError: list indices must be integers or slices, not range Anldra12 2 2,499 Apr-22-2022, 10:56 AM
Last Post: Anldra12
  string indices must be integers when parsing Json ilknurg 3 6,204 Mar-10-2022, 11:02 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020