如何将字节类对象转换为字符串对象

时间:2018-06-29 15:34:01

标签: python python-3.x matplotlib arduino-uno

componentDidUpdate

我在这里尝试从arduino获取数据,但是它的格式为import serial import numpy import matplotlib.pyplot as plt from drawnow import * data = serial.Serial('com3',115200) while True: while (data.inWaiting() == 0): pass ardstr = data.readline() print (ardstr) 。我想使用b'29.20\r\n'格式的数据,以便进行绘制。

我尝试了"29.20"ardstr = str(ardstr).strip('\r\n') 但他们都不在工作。我的python版本是3.4.3。

我怎样做才能得到ardstr.decode('UTF-8')而不是"29.40"的结果?

2 个答案:

答案 0 :(得分:3)

  

我尝试了ardstr = str(ardstr).strip('\r\n')ardstr.decode('UTF-8')

您接近了!与.strip()调用一样,使用.decode()方法返回新值。

ardstr = ardstr.strip()
ardstr = ardstr.decode('UTF-8')

答案 1 :(得分:2)

如果要单行执行,可以尝试:

ardstr = ardstr.decode('UTF-8').rstrip()

rstrip()将返回删除后缀字符的字符串副本。