如何将Python StringIO()对象传递给ZipFile(),或者它不受支持?
因此,我有一个类似文件的对象,我试图将其写入,但我得到以下类型错误:如何将Python StringIO()对象传递给ZipFile(),或者它不受支持?,python,zipfile,stringio,Python,Zipfile,Stringio,因此,我有一个类似文件的对象,我试图将其写入,但我得到以下类型错误: coercing to Unicode: need string or buffer, cStringIO.StringI found 下面是我正在使用的代码示例: file_like = StringIO() archive = zipfile.ZipFile(file_like, 'w', zipfile.ZIP_DEFLATED) # my_file is a StringIO object returned by
coercing to Unicode: need string or buffer, cStringIO.StringI found
下面是我正在使用的代码示例:
file_like = StringIO()
archive = zipfile.ZipFile(file_like, 'w', zipfile.ZIP_DEFLATED)
# my_file is a StringIO object returned by a remote file storage server.
archive.write(my_file)
文档说这是一个类似文件的类,可以接受类似文件的对象。我有什么遗漏吗?任何帮助都将不胜感激
提前谢谢 要向ZipFile添加字符串,需要使用writestr方法,并使用StringIO实例的getvalue方法从StringIO传递字符串 e、 g
注意,您还需要提供字符串的名称,以说明它在zip文件中的位置。的参数是一个文件名;你的第二行是否有输入错误,或者那是你的实际代码
ZipFile(file\u file
而不是ZipFile(file\u like
archive.writestr("name of file in zip", my_file.getvalue())