I. Jones
I. Jones

Reputation: 151

How to unzip gz file and and then use csv file?

I have been trying to unzip a .gz file and create a csv file from it. Then I need to sort that csv file. However, I keep getting errors after creating the csv file and sorting it.

I have created the file like so:

with gzip.open("test.csv.gz", 'rb') as f_in:
       with open("test.csv", 'wb') as f_out:
           shutil.copyfileobj(f_in, f_out)

After creating the file I read in the sorted csv file like this:

with open(filename, 'r') as input:
    reader = csv.DictReader(input, delimiter='\t')
    first_row = next(reader)

However I keep getting the following error from the last line of the above code.

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte

What am I doing wrong with either opening unzipping the gz file or opening the csv file?

Upvotes: 1

Views: 1096

Answers (1)

Ramsey Brenner
Ramsey Brenner

Reputation: 71

gzip's magic number is 0x1f 0x8b... so that error means it's probably still gzip'd.

Upvotes: 2

Related Questions