There are four stages of transformation.
- Python data structure (nested voice recorders, lists, strings, numbers, booleans)
- Python string containing a serialized representation of this data structure ("JSON")
- list of bytes containing a representation of this string ("UTF-8")
- list of bytes containing a representation of this previous list of bytes ("gzip")
So do these steps one by one.
import gzip import json data = [] for i in range(N): uid = "whatever%i" % i dv = [1, 2, 3] data.append({ 'what': uid, 'where': dv })
Note that adding "\n" is completely redundant here. It does not violate anything, but beyond that it is useless.
Reading works the exact same way:
with gzip.GzipFile(jsonfilename, 'r') as fin:
Of course, the steps can be combined:
with gzip.GzipFile(jsonfilename, 'w') as fout: fout.write(json.dumps(data).encode('utf-8'))
and
with gzip.GzipFile(jsonfilename, 'r') as fin: data = json.loads(fin.read().decode('utf-8'))
source share