我正在尝试获取节点下的文本和id,请参见这里的示例文件:example.xml
但是,它没有普通XML文件的结构。结构如下:
<TextWithNodes><Node id="0"/>
<Node id="1"/>
<Node id="2"/>9407011<Node id="9"/>
<Node id="10"/>ACL<Node id="13"/> <Node id="14"/>1994<Node id="18"/>
<Node id="19"/> Lg.Pr.Dc <Node id="29"/>
我想要的输出是start_node
、end_node
和text_between_node
的列表。我不确定是否可以使用lxml
库来实现这一点。你知道吗
目前,我使用
from lxml import etree
tree = etree.parse('9407011.az-scixml.xml')
nodes = tree.xpath('//TextWithNodes')[0].getchildren()
node = nodes[0] # example one node
print(node.text) # this give empty string because you don't have closing same id
使用XPath可能适合您。将
normalize-space()
与空字符串进行比较将消除没有后续文本的节点。你知道吗这可能适合您:
相关问题 更多 >
编程相关推荐