Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

python - How to compress csv file into zip archive directly?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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())

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...