How to Convert a CSV.gz to a CSV in Python?

To convert a compressed CSV file (.csv.gz) to a CSV file (.csv) and read it in your Python shell, use the gzip.open(filename, 'rt', newline='') function call to open the gzipped file, the file.read() function to read its contents, and the file.write() function to write the CSV in a normal (unzipped) file.

The gzip.open() function has the following arguments:

  • filename specifies the name of the GZipped file.
  • 'rt' opens the file in reading ('r') text ('t') mode (not binary mode)
  • newline='' prevents adding a blank line in some OS environments.

🐍 You don’t need to install the gzip module because it’s already part of Python’s standard library. 🐍

Here’s the code to read and load a GZipped CSV to Python:

import gzip


with gzip.open('my_file.csv.gz', 'rt', newline='') as csv_file:
    csv_data = csv_file.read()
    with open('my_file.csv', 'wt') as out_file:
         out_file.write(csv_data)

πŸ’‘ Note: Depending on the .csv.gz file’s encoding, you may want to pass encoding='utf-8', 'latin-1', utf-16', or your specific file encoding into gzip.open() to prevent any encoding/decoding errors.


Programming Humor