+2 votes
in Programming Languages by (74.9k points)

I am trying to read a gzipped file in my python code using the following code:

with open(metavisits_cov_file, 'r') as fin:

    for line in fin:

        lcount+=1

        vals = line.decode('utf8').strip().split(',') #for zipped file

        print (vals)

But the code is giving the following error:

Traceback (most recent call last):

  File "test.py", line 11, in <module>

    for line in fin:

    (result, consumed) = self._buffer_decode(data, self.errors, final)

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

1 Answer

+1 vote
by (353k points)
selected by
 
Best answer

I think you need to import gzip library to read the gzipped file. Make the following changes in your code and it should work.

import gzip
with gzip.open(metavisits_cov_file, 'rb') as fin:
    for line in fin:
        lcount+=1
        vals = line.decode('utf8').strip().split(',') #for zipped file
        print (vals)


...