Skip to content

Instantly share code, notes, and snippets.

@urigoren
Created September 26, 2018 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save urigoren/710f6e8ef8941556c2678863f932165f to your computer and use it in GitHub Desktop.
Save urigoren/710f6e8ef8941556c2678863f932165f to your computer and use it in GitHub Desktop.
A simple command line tool that transforms a json of format: `{"word": count}` to a readable CSV format
import json, sys
from operator import itemgetter as at
fname = sys.argv[1]
assert fname.endswith('.json')
with open(fname, 'r') as f:
d = json.load(f)
with open(fname.replace('.json', '.csv'), 'w') as f:
f.write('{k},{v}\n'.format(k="key", v="val"))
for k,v in sorted(d.items(), key=at(1), reverse=True):
f.write('"{k}",{v}\n'.format(k=k.replace('"','""'), v=v))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment