Why does pyserial for python3k return bytes while python2k returns strings?

痴心易碎 提交于 2019-12-24 02:44:39

问题


I am attempting to port https://github.com/thearn/Python-Arduino-Command-API to python 3, so far i have gotten it to the point where i can import it without error, i tried to run the blink example found here and i would always get a type error.

I think I have narrowed it down to this.

The readline function from PySerial 2.7 for python 2.7.8 returns a string and the readline function from PySerial 2.7 for python 3.3 returns bytes.

Python 2

>>> import serial
>>> serial.VERSION
'2.7'
>>> ser= serial.Serial(port='COM4')
>>> ser.readline()
'0\r\n'
>>> type(ser.readline())
<type 'str'>

Python 3

>>> import serial
>>> serial.VERSION
'2.7'
>>> ser = serial.Serial(port='COM4')
>>> ser.readline()
b'0\r\n'
>>> type(ser.readline())
<class 'bytes'>

I have checked out the source of the readline function for the python 2 and python 3 implementation of pyserial and they seem like they should both return bytes as the last line of each is return bytes(line) and thats the only return statement in the whole function.

My Question: Why does the readline function for PySerial 2.7 return different results in python2 and python3?


回答1:


This is because in Python 3.x, text is always Unicode and is represented by the str type, and binary data is represented by the bytes type. This feature differs the Python 2.x version.

In your example, ser.readline() in fact returns binary data.




回答2:


It's one of the major differences between Python 2 and 3.

From https://docs.python.org/3.0/whatsnew/3.0.html :

Python 3.0 uses the concepts of text and (binary) data instead of Unicode strings and 8-bit strings. All text is Unicode; however encoded Unicode is represented as binary data. The type used to hold text is str, the type used to hold data is bytes. The biggest difference with the 2.x situation is that any attempt to mix text and data in Python 3.0 raises TypeError, whereas if you were to mix Unicode and 8-bit strings in Python 2.x, it would work if the 8-bit string happened to contain only 7-bit (ASCII) bytes, but you would get UnicodeDecodeError if it contained non-ASCII values. This value-specific behavior has caused numerous sad faces over the years.

You will find the full explaination in the "Text Vs. Data Instead Of Unicode Vs. 8-bit" section of the link above.




回答3:


This issue came up for me when doing a socket program in python 3. When I was receiving a stream, I ended up using the decode() function for it to work correctly.

retval = sock.recv(1024).decode()

The decode was very helpful. Not sure if it's applicable to your situation, but give it a try.



来源:https://stackoverflow.com/questions/26523342/why-does-pyserial-for-python3k-return-bytes-while-python2k-returns-strings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!