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

4.5/5 - (2 votes)

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

The 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'my_file.csv.gz', 'rt', newline='') as csv_file:
    csv_data =
    with open('my_file.csv', 'wt') as out_file:

💡 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 to prevent any encoding/decoding errors.

Programming Humor