擅长:python、mysql、java
<p>您只需使用zipfile模块,然后将内容写入zip中的不同文件。在本例中,我只是将内容写入内容列表中每个项目的zip中的一个文件。我在这里也使用writestr方法,所以我不需要在磁盘上有物理文件,我只需要在内存中创建我的内容并将其写入我的zip,而不是首先在操作系统上创建一个文件,然后在zip中写入该文件</p>
<pre class="lang-py prettyprint-override"><code>from zipfile import ZipFile
with ZipFile("data.zip", "w") as my_zip:
content_arrays = ['ljfdslkfjaslkfjsdlf', 'sdfasfsdfsdfsafs']
for index, content in enumerate(content_arrays):
#do what ever you need to do here with your content
my_zip.writestr(f'file_{index}.txt', content)
</code></pre>
<p><strong>输出</strong>
<a href="https://i.stack.imgur.com/EbKcL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/EbKcL.png" alt="enter image description here"/></a></p>
<p><a href="https://i.stack.imgur.com/79Rai.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/79Rai.png" alt="enter image description here"/></a></p>