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

Saj*_*al 5 python matplotlib python-3.x arduino-uno

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)
Run Code Online (Sandbox Code Playgroud)

在这里,我试图从arduino获取数据,但它是以格式输入的b'29.20\r\n'。我想要以格式显示数据,"29.20"以便对其进行绘制。

我尝试过ardstr = str(ardstr).strip('\r\n')ardstr.decode('UTF-8') 但没有一个在工作。我的python版本是3.4.3。

我该怎么做才能得到结果"29.40"而不是"b'29.20\r\n'"

wim*_*wim 3

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

你很接近!与调用一样.strip(),使用该.decode()方法会返回新值。

ardstr = ardstr.strip()
ardstr = ardstr.decode('UTF-8')
Run Code Online (Sandbox Code Playgroud)