zip文件到字节Python 3



我想将zip文件存储在Postgres数据库中。该列是类型bytea

尝试获取JSON文件的字节或CSV文件时,我可以使用此

with open(filename, encoding='utf-8') as file_data:
    bytes_content = file_data.read()

但是,如果我尝试zip文件,甚至XLS文件,我会遇到错误。

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd7 in position 14: invalid continuation byte

我进行了一些搜索,建议更改为编码类型,我尝试了latin-1ISO-8859-1,这两个都给了我一个错误。

ValueError: A string literal cannot contain NUL (0x00) characters.

关于如何获取zip文件的字节,以便我可以将其存储在Postgres数据库中?

如果要读取JSON文件,则应以二进制模式打开文件:

with open(filename, 'rb') as file_data:
    bytes_content = file_data.read()

最新更新