从python字典到html列表 [英] From python dictionary to html list
问题描述
每个人
我的问题与这个问题非常相似: python:将字典序列化为简单的html输出
我想将包含树的python字典转换为HTML列表,使用
I would like convert my python dictionary, that contain a tree, in a HTML list that use the
<ul><li></li></ul> Tag
我想为每个元素添加一个复选框,其id =元素名称+的名称爸爸
I would like add one checkbox for each elements with the id= name of the elements + name of the dad
这是我的python字典的示例:
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': {}}}}}}
想要获得:
<ul><li><input type=checkbox id=Animalia-root>Animalia</li>
<ul><li><input type=checkbox id=Chordata-Animalia>Chordata</li>
<ul><li><input type=checkbox id=Chordata-convolvulacee>convolvulacee
</li></ul></ul></ul>
有一种简单的方法,可以在python中做到吗?谢谢
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 '{}<ul>'.format(' ' * indent)
for k,v in dictObj.iteritems():
print '{}<li><input type="checkbox" id="{}-{}">{}</li>'.format(
' ' * (indent+1), k, parent, k)
printItems(v, k, indent+1)
print '{}</ul>'.format(' ' * indent)
调整之处还在于将父键传递给 printItems()
,以便可以将其包含在< input type = checkbox ...> $的
id
属性中c $ c>标记。另一个更改是删除叶节点处理,因为您的叶用空字典表示。
The tweak was to also pass the parent key to printItems()
so that it can be included in the id
attribute for the <input type="checkbox"...>
tag. The other change was to remove the leaf node processing because your leaves are denoted by an empty dictionary.
输入:
taxonomy = {
'Animalia': {
'Chordata': {
'Mammalia': {
'Carnivora': {
'Canidae': {
'Canis': {
'coyote': {},
'dog': {}
}
},
'Felidae': {
'Felis': {
'cat': {}
},
'Panthera': {
'lion': {}
}
}
}
}
}
},
'Plantae': {
'Solanales': {
'Convolvulaceae': {
'Ipomoea': {
'sweet potato': {}
}
},
'Solanaceae': {
'Solanum': {
'potato': {},
'tomato': {}
}
}
}
}
}
printItems(taxonomy,'root',0)
的输出是:
The output of printItems(taxonomy, 'root', 0)
is:
<ul>
<li><input type="checkbox" id="Animalia-root">Animalia</li>
<ul>
<li><input type="checkbox" id="Chordata-Animalia">Chordata</li>
<ul>
<li><input type="checkbox" id="Mammalia-Chordata">Mammalia</li>
<ul>
<li><input type="checkbox" id="Carnivora-Mammalia">Carnivora</li>
<ul>
<li><input type="checkbox" id="Canidae-Carnivora">Canidae</li>
<ul>
<li><input type="checkbox" id="Canis-Canidae">Canis</li>
<ul>
<li><input type="checkbox" id="coyote-Canis">coyote</li>
<li><input type="checkbox" id="dog-Canis">dog</li>
</ul>
</ul>
<li><input type="checkbox" id="Felidae-Carnivora">Felidae</li>
<ul>
<li><input type="checkbox" id="Felis-Felidae">Felis</li>
<ul>
<li><input type="checkbox" id="cat-Felis">cat</li>
</ul>
<li><input type="checkbox" id="Panthera-Felidae">Panthera</li>
<ul>
<li><input type="checkbox" id="lion-Panthera">lion</li>
</ul>
</ul>
</ul>
</ul>
</ul>
</ul>
<li><input type="checkbox" id="Plantae-root">Plantae</li>
<ul>
<li><input type="checkbox" id="Solanales-Plantae">Solanales</li>
<ul>
<li><input type="checkbox" id="Convolvulaceae-Solanales">Convolvulaceae</li>
<ul>
<li><input type="checkbox" id="Ipomoea-Convolvulaceae">Ipomoea</li>
<ul>
<li><input type="checkbox" id="sweet potato-Ipomoea">sweet potato</li>
</ul>
</ul>
<li><input type="checkbox" id="Solanaceae-Solanales">Solanaceae</li>
<ul>
<li><input type="checkbox" id="Solanum-Solanaceae">Solanum</li>
<ul>
<li><input type="checkbox" id="tomato-Solanum">tomato</li>
<li><input type="checkbox" id="potato-Solanum">potato</li>
</ul>
</ul>
</ul>
</ul>
</ul>
这篇关于从python字典到html列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!