我经常使用len(find_all(“some_元素”)来计算xml文件中的实体数。我尝试构建一个函数,但它不起作用/它总是给我“None”
XML文件:
<parent>
<some>
<child>text</child>
<child>text</child>
<child>text</child>
</some>
</parent>
我的python代码:
def return_len(para1,para2): # doesn't work
if bool(suppe.para1): # the element isn't always present in the xml
return len(suppe.para1.find_all(para2))
def return_len1(): # does work
if bool(suppe.some):
return len(suppe.some.find_all("child"))
print(return_len("some","child")) # doesnt work
print(return_len1()) # does work
我必须如何修改我的函数return\u len才能工作/我做错了什么
没有任何外部库-请参阅下面的
输出
你可以这样做
相关问题 更多 >
编程相关推荐