Here is a great answer about how to use json.dumps
to write to a gzip file. What I would like to do is to use the dump
method instead to serialize the json directly into a GzipFile
object.
Example code:
import gzip, json
data = # a dictionary of data here
with gzip.open(write_file, 'w') as zipfile:
json.dump(data, zipfile)
The error raised is
TypeError: memoryview: a bytes-like objet is required, not 'str'
I believe this is caused because the gzip write() method wants a bytes object passed to it. Per the documentation,
The json module always produces str objects, not bytes objects. Therefore, fp.write() must support str input.
Is there a way to wrap the json
string output as bytes so that GzipFile
's write()
will handle it? Or is the only way to do this to use json.dumps
and encode()
the resulting string into a bytes object, as in the other linked answer?