Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将Python StringIO()对象传递给ZipFile(),或者它不受支持?_Python_Zipfile_Stringio - Fatal编程技术网

如何将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())