Python Forum

Full Version: String error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey there im not getting the expected result

i expect two numbers temperture and humidty seperated by a comma

here is my error:

line 25, in <module>
dataArray = arduinoString.split(",")

TypeError: a bytes-like object is required, not 'str'

my python code
import serial
import numpy
import matplotlib.pyplot as plt
#from drawnow import 



arduinoData = serial.Serial('com3', 9600)



while True:
    while (arduinoData.inWaiting() ==0):
        pass
     
    arduinoString = arduinoData.readline()
    dataArray = arduinoString.split(",")
    temp = float(dataArray[0])
    humid = float(dataArray[1])
    
    print (temp, ",", humid)
  
what i Expected:

70 , 50

using python 3.7
How are you initializing dataArray ?

arduinoString = '98.6, 89'
dataArray = arduinoString.split(",")
temp = float(dataArray[0])
humid = float(dataArray[1])
print (temp, ",", humid)
This code works because dataArray is an ordinary, run-of-the-mill python list and is not expecting a bytes like object.
have you checked what kind of data you get on the serial port?
    arduinoString = arduinoData.readline()
    print(arduinoString) # check the output
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,
serial.Serial will return bytes.
So in Python 3 can never mix str and bytes,two solution continue to work with bytes or convert to string decode().

Make error
>>> arduinoString = b'data,123'
>>> arduinoString.split(",")
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
TypeError: a bytes-like object is required, not 'str' 
Continue with bytes.
>>> arduinoString = b'data,123'
>>> arduinoString.split(b",")
[b'data', b'123']
Convert to str.
>>> arduinoString = b'data,123'
>>> arduinoString = arduinoString.decode()
>>> arduinoString
'data,123'
>>> arduinoString.split(",")
['data', '123']
(Jan-24-2021, 10:02 PM)Serafim Wrote: [ -> ]have you checked what kind of data you get on the serial port?
    arduinoString = arduinoData.readline()
    print(arduinoString) # check the output

About the same thing,, i was down this path to begin with your correct this is a good place to start

this is what i got:
b'63,71\r\n'
i had a hunch it was a mismatch of data
(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 becuase the tutorial i was following next puts this inot a matplot chart and that was my goal cheers.

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
(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.