Compress CSV File using Python

This topic elaborates on how to compress CSV file using Python. It explains the IDE settings, the stepwise process, and a working code sample for CSV file compression using Python. Moreover, you can use the same approach to compress multiple CSV files into a single ZIP archive.

Steps to Compress CSV using Python

  1. Prepare the IDE to work with Aspose.ZIP for comma-separated value file compression
  2. Make an object of the Archive class
  3. Pass the string values as the name of files in the ZIP
  4. Export the output compressed ZIP directory using the save method

The steps above show how to create a CSV file size reducer using Python. The process begins by reading the source comma-separated value file. Subsequently, pass the file path and names to create the entries in the output ZIP directory. Finally, write the generated ZIP archive containing the compressed CSV file.

Code to Compress CSV File using Python

import aspose.zip as az
path = "C://"
# Create and save archive with a CSV file
with az.Archive() as archive:
# Add CSV file into archive
archive.create_entry("Sample.csv", path + "input.csv")
# Save zip archive
archive.save('csv_archive.zip')

The code sample below showcases the feature to compress CSV using Python. It is a simple version to learn the compression which can further be improvised to compress several CSV files at once. Likewise, complicated and advanced ZIP file processing features like protecting the ZIP directory can also be incorporated into your application based on your requirements.

This article has outlined the process to compress a CSV file using Python. However, if you need to explore extracting ZIP files, then take a look at Extract ZIP File in Python.

 English