+2 votes
in Programming Languages by (8.1k 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

0 votes
by (15.8k points)

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)

...