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

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -