Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
String error
#4
When you have a regular string, you pass in another string object as text to split on.

>>> "split this, string, on the, commas".split(",")
['split this', ' string', ' on the', ' commas']
But if instead of a string you have a bytes object, this won't work.
>>> b"split this, string, on the, commas".split(",")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: a bytes-like object is required, not 'str'
Your arduinoStringis a bytes object. So to split it, the argument has to be a bytes object as well. Instead of ",", use b"," OR convert it to a string and split it as normal.

>>> arduinoString = b"split this, string, on the, commas"
>>> arduinoString.split(b",")  # split directly on the bytes object
[b'split this', b' string', b' on the', b' commas']

>>> arduinoString.decode().split(",")   # decode to str, then split the str
['split this', ' string', ' on the', ' commas']
Note the result of the split is a list of bytes objects,
Kurta likes this post
Reply


Messages In This Thread
String error - by Kurta - Jan-24-2021, 07:27 PM
RE: String error - by BashBedlam - Jan-24-2021, 07:45 PM
RE: String error - by Serafim - Jan-24-2021, 10:02 PM
RE: String error - by Kurta - Jan-24-2021, 10:41 PM
RE: String error - by bowlofred - Jan-24-2021, 10:13 PM
RE: Arduino toString error - by Kurta - Jan-24-2021, 10:50 PM
RE: String error - by snippsat - Jan-24-2021, 10:27 PM

Forum Jump:

User Panel Messages

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