python - Create file from bytes stored as string -
i have use case getting blob database have convert binary file. python stores string although bytes. when try write binary type error, str not accepted.
i wrote sample code reproduce variable looks like. have seen sample code in other languages task rather easy. in solving problem using python 3 appreciated. simple missing , unable find answer online.
import binascii f = open('test.xlsx', 'rb') content = binascii.hexlify(f.read()) f.close output = content.decode("utf-8") #output2 = hex(int(output, 16)) f = open('temp', 'wb') f.write(output) #typeerror: bytes-like object required, not 'str' f.close() #convert type bytes , recreate file
you can write:
f.write(bytearray(output,"utf-8"))
instead of:
f.write(output) #typeerror: bytes-like object required, not 'str'
to solve problem. although seems not pythonic way it.
Comments
Post a Comment