简体   繁体   中英

How to read back the bytes file in python?

In Python, I have saved a model as joblib file, and I read the joblib as bytes using

bytes_data = open('model.joblib','rb').read()

Then I convert to base64 using below code and store in a database

import base64
base64_data = base64.b64encode(bytes_data)

Later I load the base64_data from database and decode back to binary

loaded_binary = base64.b64decode(base64_data)

Now I am writing the binary file back

bytes_load = open(loaded_binary,'wb').write()

Here I am getting Error

Traceback (most recent call last):

  File "<ipython-input-28-1d2986913b15>", line 1, in <module>
    bytes_load = open(loaded_binary,'wb').write()

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte

reading and writing binary doesn't allowing the encoding arguments, I need to get back the same model.

You pass decoded binary file content as file name into open function. I guess instead of

bytes_load = open(loaded_binary,'wb').write()

You need:

bytes_load = open('somefilename', 'wb').write(loaded_binary)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM