Convert Stringed Bytes Back Into Bytes
I'm working on a project which saves some bytes as a string, but I can't seem to figure out how to get the bytes back to a actual bytes! I've got this string: 'b'\x80\x03]q\x00(X\r
Solution 1:
Try:
x="b'\x80\x03]q\x00(X\r\x00\x00\x00My First Noteq\x01X\x0e\x00\x00\x00My Second Noteq\x02e.'"
y=x[2:-1].encode("utf-8")
>>> print(y)
b'\xc2\x80\x03]q\x00(X\r\x00\x00\x00My First Noteq\x01X\x0e\x00\x00\x00My Second Noteq\x02e.'>>> print(type(y))
<class'bytes'>
You have just bytes
converted to regular string
without encoding - so you have redundant tags indicating that: b'...'
- you just need to drop them and python will do the rest for you ;)
Post a Comment for "Convert Stringed Bytes Back Into Bytes"