python: serialize a dictionary into a simple html output

前端 未结 9 658
轮回少年
轮回少年 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:30

    Look at my implementation:

    def pretty_items(r, d, nametag="%s: ", itemtag='
  • %s
  • ', valuetag="%s", blocktag=('
      ', '
    ')): if isinstance(d, dict): r.append(blocktag[0]) for k, v in d.iteritems(): name = nametag % k if isinstance(v, dict) or isinstance(v, list): r.append(itemtag % name) pretty_items(r, v) else: value = valuetag % v r.append(itemtag % (name + value)) r.append(blocktag[1]) elif isinstance(d, list): r.append(blocktag[0]) for i in d: if isinstance(i, dict) or isinstance(i, list): r.append(itemtag % " - ") pretty_items(r, i) else: r.append(itemtag % i) r.append(blocktag[1])

    Will output all items in HTML format using

      and
    • tags. And is also optional to change the tags. And then, just use CSS to handle with the indentation.

提交回复
热议问题