How to compress csv file into zip archive directly?

可紊 提交于 2019-12-10 03:07:13

问题


I am generating a number of csv files dynamically, using the following code:

import csv
fieldnames = ['foo1', 'foo2', 'foo3', 'foo4']
with open(csvfilepath, 'wb') as csvfile:
    csvwrite = csv.DictWriter(csvfile, delimiter=',', fieldnames=fieldnames)
    csvwrite.writeheader()
    for row in data:
        csvwrite.writerow(row)

To save space, I want to compress them.
Using the gzip module is quite easy:

with gzip.open("foo.gz", "w") as csvfile :
    csvwrite = csv.DictWriter(csvfile, delimiter=',', fieldnames=fieldnames)
    csvwrite.writeheader()
    for row in data:
        csvwrite.writerow(row)

But I want the file in 'zip' format.

I tried the zipfile module, but I am unable to directly write files into the zip archive.

Instead, I have to write the csv file to disk, compress them in a zip file using following code, and then delete the csv file.

with ZipFile(zipfilepath, 'w') as zipfile:
    zipfile.write(csvfilepath, csvfilename, ZIP_DEFLATED)

How can I write a csv file directly to a compressed zip similar to gzip?


回答1:


Use the cStringIO.StringIO object to imitate a file:

with ZipFile(your_zip_file, 'w', ZIP_DEFLATED) as zip_file:
    string_buffer = StringIO()
    writer = csv.writer(string_buffer)

    # Write data using the writer object.

    zip_file.writestr(filename + '.csv', string_buffer.getvalue())



回答2:


Thanks kroolik It's done with little modification.

with ZipFile(your_zip_file, 'w', ZIP_DEFLATED) as zip_file:
    string_buffer = StringIO()
    csvwriter = csv.DictWriter(string_buffer, delimiter=',', fieldnames=fieldnames)
    csvwrite.writeheader()
    for row in cdrdata:
        csvwrite.writerow(row)
    zip_file.writestr(filename + '.csv', string_buffer.getvalue())


来源:https://stackoverflow.com/questions/25971205/how-to-compress-csv-file-into-zip-archive-directly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!