python: serialize a dictionary into a simple html output

前端 未结 9 774
轮回少年
轮回少年 2021-01-12 00:59

using app engine - yes i know all about django templates and other template engines.

Lets say i have a dictionary or a simple object, i dont know its structure and i

9条回答
  •  轮回少年
    2021-01-12 01:17

    import pprint
    
    
    pprint.pprint(yourDict)
    

    Well, no HTML, but similar to your for/print approach.

    EDIT: or use:

    niceText = pprint.pformat(yourDict)
    

    this will give you the same nice output with all indents, etc. Now you can iterate over lines and format it into HTML:

    htmlLines = []
    for textLine in pprint.pformat(yourDict).splitlines():
        htmlLines.append('
    %s' % textLine) # or something even nicer htmlText = '\n'.join(htmlLines)

提交回复
热议问题