Decode the bytes object to produce a string:
>>> b"abcde".decode("utf-8")
'abcde'
The above example assumes that the bytes object is in UTF-8, because it is a common encoding. However, you should use the encoding your data is actually in!
Decode the bytes object to produce a string:
>>> b"abcde".decode("utf-8")
'abcde'
The above example assumes that the bytes object is in UTF-8, because it is a common encoding. However, you should use the encoding your data is actually in!
Decode the byte string and turn it in to a character (Unicode) string.
Python 3:
encoding = 'utf-8'
b'hello'.decode(encoding)
or
str(b'hello', encoding)
Python 2:
encoding = 'utf-8'
'hello'.decode(encoding)
or
unicode('hello', encoding)
[Python 3] Understanding the difference between byte string and strings.
How do I convert a string to bytes?
How to convert bytes to string in python?
Converting bytes to string to bytes
I've been reading a bit about how python 3 strings are based around unicode and I ran into the the concept of a byte string.
Here's the definition of a byte string I've taken from stackoverflow:
A byte string is a sequence of bytes. It isn't human-readable. Under the hood, everything must be converted to a byte string before it can be stored in a computer.
What I don't understand is if the normal python string type is just a sequence of Unicode characters, which at the low level is stored as bytes, why does a byte string even need to exist? Since at the low level a normal python string is just storing the Unicode sequence as bytes anyways? How is a byte string not redundant?
Suppose I something like
s = "GW\x25\001"
How do I convert that string to bytes, interpreting the backslashes as escapes? In other words, the resulting byte array should be of length 4.
UPDATE: Hmmm, I was taking the string from sys.argv[1], which seems to complicate things and not make it turn out as expected. So I'm still not sure what the answer is.
i am stuck in python programming. i read a line from the serial port
line=ser.readline() print(line)
when the above code is executed i get like
b'-83,-156,-205\r\n'
but i want only values like a=-83,b=-156,c=-205. how to extract these values/ how to get these values?