简体   繁体   English

如何读回python中的字节文件?

[英]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在 Python 中,我将模型保存为 joblib 文件,并使用

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

Then I convert to base64 using below code and store in a database然后我使用下面的代码转换为 base64 并存储在数据库中

import base64
base64_data = base64.b64encode(bytes_data)

Later I load the base64_data from database and decode back to binary后来我从数据库加载 base64_data 并解码回二进制

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.您将解码的二进制文件内容作为文件名传递给 open 函数。 I guess instead of我想而不是

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

You need:你需要:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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