Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
String error
#8
Exclamation 
(Jan-24-2021, 10:13 PM)bowlofred Wrote: 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,

This is actually what i needed and worked. I had convert to a number because the tutorial i was following.. puts this into a matplot chart and that was my goal.

for any one else trying to do this here is the link ..Arduino-python to matplot
Note: I use Anaconda with python 3.7 this seems to work for me up to this point. in the tutorial he uses 2.7.
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: Arduino to Python to Matplot String error - by Kurta - Jan-24-2021, 10:54 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