I was wondering if it is possible to convert a byte string which I got from reading a file to a string (so type(output) == str
). All I've found on Google so far has been answers like How do you base-64 encode a PNG image for use in a data-uri in a CSS file?, which does seem like it would work in python 2 (where, if I'm not mistaken, strings were byte strings anyway), but which doesn't work in python 3.4 anymore.
The reason I want to convert this resulting byte string to a normal string is that I want to use this base64-encoded data to store in a JSON object, but I keep getting an error similar to:
TypeError: b'Zm9v' is not JSON serializable
Here's a minimal example of where it goes wrong:
import base64
import json
data = b'foo'
myObj = [base64.b64encode(data)]
json_str = json.dumps(myObj)
So my question is: is there a way to convert this object of type bytes
to an object of type str
while still keeping the base64-encoding (so in this example, I want the result to be ["Zm9v"]
. Is this possible?
Try this:
def bytes_to_base64_string(value: bytes) -> str:
import base64
return base64.b64encode(value).decode('ASCII')
There is one misunderstanding often made, especially by people coming from the Java world. The bytes.decode('ASCII')
actually encodes bytes to string, not decodes them.
Try
data = b'foo'.decode('UTF-8')
instead of
data = b'foo'
to convert it into a string.
What works for me is to change the b64encode
line to:
myObj = [base64.b64encode(data).decode('ascii')]
This is explained in https://stackoverflow.com/a/42776711 :
base64 has been intentionally classified as a binary transform.... It was a design decision in Python 3 to force the separation of bytes and text and prohibit implicit transformations.
The accepted answer doesn't work for me (Python 3.9) and gives the error:
Traceback (most recent call last):
File "/tmp/x.py", line 4, in <module>
myObj = [base64.b64encode(data)]
File "/usr/lib64/python3.9/base64.py", line 58, in b64encode
encoded = binascii.b2a_base64(s, newline=False)
TypeError: a bytes-like object is required, not 'str'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With