html分析python字典_从python字典到html列表

I would like convert my python dictionary, that contain a tree, in a HTML list that use the

I would like add one checkbox for each elements with the id= name of the elements + name of the dad

this is an example of my python dictionary:

{'Animalia': {'Chordata': {'Mammalia': {'Carnivora': {'Canidae': {'Canis': {'coyote': {},

'dog': {}}},

'Felidae': {'Felis': {'cat': {}},

'Panthera': {'lion': {}}}}}}},

'Plantae': {'Solanales': {'Convolvulaceae': {'Ipomoea': {'sweet potato': {}}},

'Solanaceae': {'Solanum': {'potato': {},

'tomato': {}}}}}}

And I would like obtain:

  • Animalia
    • Chordata
      • convolvulacee

There is an easy way, to do this in python? Thank you

解决方案

A trivial couple of tweaks to the answer that you referenced will give you the result that you want:

def printItems(dictObj, parent, indent):

if len(dictObj):

print '{}

  • '.format(' ' * indent)

for k,v in dictObj.iteritems():

print '{}

{}'.format(

' ' * (indent+1), k, parent, k)

printItems(v, k, indent+1)

print '{}

'.format(' ' * indent)

The tweak was to also pass the parent key to printItems() so that it can be included in the id attribute for the tag. The other change was to remove the leaf node processing because your leaves are denoted by an empty dictionary.

For input:

taxonomy = {

'Animalia': {

'Chordata': {

'Mammalia': {

'Carnivora': {

'Canidae': {

'Canis': {

'coyote': {},

'dog': {}

}

},

'Felidae': {

'Felis': {

'cat': {}

},

'Panthera': {

'lion': {}

}

}

}

}

}

},

'Plantae': {

'Solanales': {

'Convolvulaceae': {

'Ipomoea': {

'sweet potato': {}

}

},

'Solanaceae': {

'Solanum': {

'potato': {},

'tomato': {}

}

}

}

}

}

The output of printItems(taxonomy, 'root', 0) is:

  • Animalia
    • Chordata
      • Mammalia
        • Carnivora
          • Canidae
            • Canis
              • coyote
              • dog
          • Felidae
            • Felis
              • cat
            • Panthera
              • lion
  • Plantae
    • Solanales
      • Convolvulaceae
        • Ipomoea
          • sweet potato
      • Solanaceae
        • Solanum
          • tomato
          • potato
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值