Thursday 15 August 2013

Pretty-printing JSON with ASCII color in python -



Pretty-printing JSON with ASCII color in python -

i'm looking print json command line, in python, ascii colors. example, (excellent) jq utility color-ify json using bold ascii colors so:

input: curl --silent http://coinabul.com/api.php | jq . output:

does know how accomplish effect python? couple of questions provide info on using ascii colors python (e.g. print in terminal colors using python?), effect requires combining pretty-printing machinery colorify-ing machinery in different way, think.

this should started (it prints keys in blue):

import json import urllib2 # ansi color terminal escape sequences okblue = '\033[94m' endc = '\033[0m' def pretty(keyvals, indent=''): print '{' key, val in keyvals.iteritems(): print '{} {}"{}"{}:'.format(indent, okblue, key, endc), if isinstance(val, dict): pretty(val, indent + ' ') elif isinstance(val, str): print '"{}",'.format(val) else: print '{},'.format(val) print indent + '},' req = urllib2.request('http://coinabul.com/api.php', headers={ 'user-agent': 'mozilla/5.0', }) page = urllib2.urlopen(req) parsed = json.load(page) pretty(parsed)

python json pretty-print

No comments:

Post a Comment