我有一个StringIO()文件状对象,我正在尝试将其写入ZipFile(),但我得到这个类型错误:
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)
文档说StringIO()是一个类似文件的类ZipFile()可以接受类似文件的对象。我有什么遗漏的吗?
要将字符串添加到 ZipFile,您需要使用 writestr 方法并使用 StringIO 实例的 getvalue 方法从 StringIO 传递字符串
e.g.
archive.writestr("name of file in zip", my_file.getvalue())
请注意,您还需要给出字符串的名称,以表明它在 zip 文件中的放置位置。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)