简体   繁体   English

像对象这样的字节是必需的,而在python 3 parsegps(str)中不是字符串

[英]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.

相关问题 typeError:需要一个类似字节的对象,而不是 Python 3 中的“str” - typeError: a byte-like object is required, not 'str' in Python 3 字符串 - Python 3.5 需要写一个类似字节的对象,而不是“str” - string - Python 3.5 write a bytes-like object is required, not 'str' Python-需要像对象这样的字节,而不是str - Python - a bytes like object is required, not str 使用 Python 3 A byte-like object is required not a str" 我尝试将其转换为字节,但它仍然给我同样的错误 - using Python 3 A byte-like object is required not a str" i have tried converting it into bytes but it still giving me the same error 类型错误:将字符串写入文件需要类似字节的 object,而不是“str” - TypeError: a bytes-like object is required, not 'str', for writing string to file TypeError:需要一个类似字节的对象,而不是python 3.5.2和pytesseract中的“ str” - TypeError: a bytes-like object is required, not 'str' in python 3.5.2 and pytesseract Python 3.7 TypeError:需要类似字节的对象,而不是“str” - Python 3.7 TypeError: a bytes-like object is required, not 'str' python 3.5:“ TypeError:memoryview:需要一个类似字节的对象,而不是'str'” - python 3.5 : “TypeError: memoryview: a bytes-like object is required, not 'str'” Python 3升级,需要一个类似字节的对象,而不是'str' - Python 3 upgrade, a bytes-like object is required, not 'str' Python错误:TypeError:需要一个类似字节的对象,而不是'str' - Python error: TypeError: a bytes-like object is required, not 'str'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM