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
  TypeError: int() argument must be a string, a bytes-like object or a number, not 'Non Anldra12 2 794 May-02-2021, 03:45 PM
Last Post: Anldra12
  TypeError: __str__ returned non-string (type tuple) Anldra12 1 997 Apr-13-2021, 07:50 AM
Last Post: Anldra12
  list indices must be integers or slices, not lists error djwilson0495 2 833 Aug-27-2020, 06:13 PM
Last Post: deanhystad
  [Solved]TypeError: list indices must be integers or slices, not str NectDz 3 1,542 Jun-02-2020, 08:21 AM
Last Post: DreamingInsanity
  TypeError: __repr__ returned non-string (type dict) shockwave 0 1,132 May-17-2020, 05:56 PM
Last Post: shockwave
  HELP: String indices must be integers animrehrm 6 1,397 May-17-2020, 02:18 PM
Last Post: animrehrm
  Why, TypeError: expected string or bytes-like object ? JohnnyCoffee 3 7,984 May-08-2020, 04:26 AM
Last Post: bowlofred
  string indices must be integers constantin01 6 1,114 Apr-22-2020, 10:30 AM
Last Post: buran
  TypeError: list indices must be integers or slices, not float hissonrr 3 1,263 Apr-19-2020, 12:02 AM
Last Post: hissonrr
  How do I convert this string back to a list of integers? donmerch 6 1,371 Apr-05-2020, 06:43 PM
Last Post: donmerch

Forum Jump:

User Panel Messages

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