0

I have '\n' and '\r' in my readline data which is coming from Microcontroller. I have tried to clean them through the following code but still I have b'' in every line. Is there any way to remove it completely from the data ?

bs = ser.readline()
bs = bs.replace(b'\n', b' ').replace(b'\r', b' ')
print(bs)
S.B
  • 13,077
  • 10
  • 22
  • 49
engr_john
  • 27
  • 2

2 Answers2

3

b'' means it is sequence of bytes not string, you can .decode()(utf-8 by default) it to convert it to python string. also use .strip() without arguments, it will remove leading and trailing whitespaces from the string.

ser.readline().decode().strip()

If '\n' and '\r' exists in the middle of the string you can use either .replace() or .translate() method.

S.B
  • 13,077
  • 10
  • 22
  • 49
0

b'' in your string shows it's a Bytes literals. you need to decode it into utf-8 format

print(bs.decode('utf-8'))