Skip to content

Instantly share code, notes, and snippets.

@mvanveen
Created August 10, 2011 19:29
Show Gist options
  • Save mvanveen/1137900 to your computer and use it in GitHub Desktop.
Save mvanveen/1137900 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
''''
NOTE: on dev_appserver.py console needs a cast to a dict() like so:
pending_dict = dict(pending_activity)
sometimes datatype passed back up to the method for recursing is not an array or a dict, but a float, say
which has no len() method and raises TypeError, thus the try except block
'''
import json
def print_list_dict(indent, myinput):
if isinstance(myinput, list):
for e in myinput:
if not isinstance(e, dict):
print '%s%s' % (indent, str(e).encode('ascii', 'ignore')) or print_list_dict(indent,e)
else:
print_list_dict(indent, e)
elif isinstance(myinput, dict):
for k, v in myinput.iteritems():
if isinstance(v, dict) or isinstance(v, list):
print ("%s%s: " % (indent, str(k).encode('ascii', 'ignore')))
print_list_dict("   " + indent, v)
if isinstance(v, dict):
print
else:
print ("%s%s: %s" % (
indent,
str(k).encode('ascii', 'ignore'),
str(v).encode('ascii', 'ignore'))
)
if __name__ == '__main__':
import sys
with open(sys.argv[1], 'r') as fileObj:
dictObj = json.loads(fileObj.read())
print_list_dict("", dictObj)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment