[英]How to convert the byte class object into a string object
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)
在這里,我試圖從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'"
?
我嘗試了
ardstr = str(ardstr).strip('\\r\\n')
和ardstr.decode('UTF-8')
你近了! 與.strip()
調用一樣,使用.decode()
方法將返回新值。
ardstr = ardstr.strip()
ardstr = ardstr.decode('UTF-8')
如果要單行執行,可以嘗試:
ardstr = ardstr.decode('UTF-8').rstrip()
rstrip()
將返回字符串的副本,其中刪除了尾隨字符。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.