I'm new to python 3 and trying to extract a message from a bytes array that contains both string and bytes in the message.
I'm unable to extract the bytes message from the decoded bytes array.
- Firstly, I decode the bytes array.
- Then I do a split on the decoded array.
- I get string values upon splitting the array.
I tried to use bytes(v) for v in rest.split()
function to try and get the bytes array and then decode it, but wasn't able to.
# The message chunk:
chunk = b"1568077849\n522\nb'l5:d4:auth53:\xc3\x99\xc3\xac\x1fH\xc2\xa3ei6eli1eee'\n"
# I split the chunk into sub categories for further processing:
_, size, rest = (chunk.decode("utf-8")).split('\n', 2)
# _ contains "1568077849"
# size contains "522"
# rest contains "b'l5:d4:auth53:\xc3\x99\xc3\xac\x1fH\xc2\xa3ei6eli1eee'"
I'm supposed to be able to decode the rest variable (rest.decode("utf-8")), but since it's getting stored as string, I'm having a hard time figuring out how can I convert that to bytes and then decode the value.
The expected result: l5:d4:auth53:ÙìH£ei6eli1eee