【发布时间】:2021-12-07 07:29:50
【问题描述】:
我经常使用 len(find_all("some_element") 来计算 xml 文件中的实体数量。我尝试构建一个函数,但它不起作用/它总是给我“无”。
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_len 才能开始工作/我做错了什么?
【问题讨论】:
-
我相信 suppe 是你的 Beautiful Soup 对象。你能分享一下你的实现吗?
标签: python xml beautifulsoup