Archive
Posts Tagged ‘gzip’
opening gzipped JSON files
January 7, 2015
Leave a comment
Problem
I have a project where the input JSON file is almost 7 MB. I keep this project in my Dropbox folder, so that 7 MB text file seems to be a waste. Any way to reduce its size?
Solution
I zipped it up with gzip: “gzip -9 input.json“. This command produced a 1.3 MB “input.json.gz” file and deleted the original. Good. But how to open it in Python?
Normal way (without gzip):
import json
with open("input.json") as f:
d = json.load(f)
Compressed way (with gzip):
import json
import gzip
with gzip.open("input.json.gz", "rb") as f:
d = json.loads(f.read().decode("ascii"))
I didn’t notice any performance penalty. The application that first reads this json file starts as fast as before.
