using the application engine - yes, I know everything about django templates and other template mechanisms.
Let's say I have a dictionary or a simple object, I donβt know its structure, and I want to serialize it in html.
so if i had
{'data':{'id':1,'title':'home','address':{'street':'some road','city':'anycity','postal':'somepostal'}}}
want, I want, this is visualized in some form of readable html using lists or tables;
data: id:1 title:home address: street: some road city: anycity postal:somepostal
Now i know what i can do
for key in dict.items print dict[key]
but they will not plunge into child values ββand enumerate each key pair when the key / value is a dictionary, that is, the dict address.
Is their module for python, which is easy / fast, which will make it beautiful. or does anyone have a simple code that they can insert that can do this.
Solution All the solutions here were helpful. pprint is without a doubt a more stable means of printing a dictionary, although it has nothing to return about html. Although you can still print.
I am done with this now:
def printitems(dictObj, indent=0): p=[] p.append('<ul>\n') for k,v in dictObj.iteritems(): if isinstance(v, dict): p.append('<li>'+ k+ ':') p.append(printitems(v)) p.append('</li>') else: p.append('<li>'+ k+ ':'+ v+ '</li>') p.append('</ul>\n') return '\n'.join(p)
It converts the dict into unordered lists, which are currently in order. some css and maybe a little tweak should make it readable.
I'm going to reward the answer to the person who wrote the code above, I made a couple of small changes, because the unordered lists were not nested. I hope everyone agrees that many of the proposed solutions have proven useful, but the code above displays the true html representation of the dictionary, even if it's rude.