Read file saved using gzip: why is the file handle a string?

Viewed 22

I have a list of lists, let's suppose it is:

sentcorpus = [["hello", "how", "are", "you", "?"], ["hello", "I", "'m", "fine"]]

I want to save it in gzip format:

import gzip
import json
with gzip.open('corpus.json.gz', 'wb') as fileh:
    fileh.write(json.dumps(sentcorpus).encode("utf8"))

Then it would be logical to read it back like this:

with gzip.open('wbec_corpus.json.gz', 'rb') as fileh:
    sentcorpus = json.load(fileh.read().decode("utf8"))

But no:

AttributeError: 'str' object has no attribute 'read'

Instead this one works:

with gzip.open('wbec_corpus.json.gz', 'rb') as fileh:
    sentcorpus = json.load(fileh)

Why is fileh a string and not a file handle?

1 Answers

It's not the file object, JSON library is throwing the error. To understand we need to look at json.load and JSON.loads

json.load(fp, **)

Deserialize fp (a .read()-supporting text file or binary file containing a JSON document) to a Python object using this conversion table.

json.loads(s, )

Deserialize s (a str, bytes or bytearray instance containing a JSON document) to a Python object using this conversion table.

In short JSON.load doesn't require .read object it needs a file pointer; however JSON.loads does require string or file.read() .

SO both of these line below will work

sentcorpus = json.loads(fileh.read().decode("utf8"))
sentcorpus = json.load(fileh)
Related