[英]a byte like object is required not string in python 3 parsegps(str)
am trying to write a code that well read data from my serial UART and parse the NMEA gps sentence by using pynmea2 module i was running this code in python 2 and it worked like magic ,when i tried to run it with python3 a type error rise am using python idle to write the code in my raspberry pi 3 and all the hardware between neo 6m gps and the raspberry are fine my code is blow ` 我试图编写一个可以很好地从我的串行UART读取数据并使用pynmea2模块解析NMEA gps语句的代码,而我在python 2中运行此代码,当我尝试使用python3运行它时,它就像魔术一样工作,类型错误上升我正在使用python idle在我的raspberry pi 3中编写代码,而neo 6m gps和raspberry之间的所有硬件都很好,我的代码很糟糕
import serial
import pynmea2
def parseGPS(str):
if str.find('GGA') > 0:
msg = pynmea2.parse(str)
#print "Timestamp: %s -- Lat: %s %s -- Lon: %s %s -- Altitude: %s %s" % (msg.timestamp,msg.lat,msg.lat_dir,msg.lon,msg.lon_dir,msg.altitude,msg.altitude_units)
serialPort = serial.Serial("/dev/ttyS0", 9600, timeout=0.5)
while True:
str = serialPort.readline()
parseGPS(str)
` `
and i get this Messag 我得到这个消息
"if str.find('GGA').0:
TypeError:'a bytes-like object is required, not 'str' "
In Python 3.x, text is always Unicode and is represented by the str type, and binary data is represented by the bytes type. 在Python 3.x中,文本始终是Unicode,并由str类型表示,而二进制数据则由字节类型表示。 serial.readline() in fact returns binary data, and therefor in bytes type.
serial.readline()实际上返回二进制数据,因此返回字节类型。 This is different from Python 2.x.
这与Python 2.x不同。
You can convert the encoded bytes data into str with: 您可以使用以下命令将编码的字节数据转换为str:
str = serailPort.readline().decode()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.