How to compress csv file into zip archive directly

2019-04-23 18:15发布

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?

2条回答
女痞
2楼-- · 2019-04-23 18:53

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())
查看更多
虎瘦雄心在
3楼-- · 2019-04-23 18:57

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())
查看更多
登录 后发表回答