我有以下 xml 文件:
<components version="1.0.0">
<component type="foo">
<sample>Foo<sample>
<sample lang=a>abc</sample>
<sample lang=b>efj</sample>
</component>
</components>
from lxml import etree
def parse(path: str):
return etree.parse(path)
def components(path: str) -> list:
components = parse_appinfo_xml(path).xpath("/components/component")
return list(components)
def sample(path: str) -> str:
sample = components(path)[0].find("sample").text
return str(sample)
path = "test.xml"
print(sample(path))
我想遍历所有
sample
标签并获取没有 lang
属性的标签的值,即。第一个。我该怎么做呢?我知道我需要使用 for 循环,但不确定如何检查 lang
是否存在。
您可以检查
lang
是否不在标签的属性中:
from lxml import etree
xml_string = """
<components version="1.0.0">
<component type="foo">
<sample>Foo</sample>
<sample lang="a">abc</sample>
<sample lang="b">efj</sample>
</component>
</components>
"""
root = etree.fromstring(xml_string)
for sample in root.findall("component/sample"):
if "lang" not in sample.attrib:
print(sample.text)
打印:
Foo