+3 votes
in Programming Languages by (3.7k points)
edited by

I have the following code to read a gzipped file in python and it has been working without any error in Python 2. I installed Python 3 and now am getting error: TypeError: a bytes-like object is required, not 'str'. I am not sure what needs to be changed to fix this issue?

    with gzip.open(wv.inpfile, 'rb') as fin:
        for line in fin:
            if (line_count == 0):
                line_count+=1
            else:
                record_columns = line.strip('\n').split('\t')

1 Answer

0 votes
by (15.8k points)

Python 3 added a dedicated bytes type, but unlike Python 2, no automatic coercion between bytes and Unicode is provided (check this link for the details: https://www.python.org/dev/peps/pep-0404/#strings-and-bytes). You are reading the file in binary mode and trying to use strip() and split() on the record. That's why you are getting error.

There may be several solutions, but decoding the record works like a charm. Try the following code to fix the issue.

    with gzip.open(wv.inpfile, 'rb') as fin:
        for line in fin:
            if (line_count == 0):
                line_count+=1
            else:
                record_columns = line.decode('utf8').strip('\n').split('\t')

...