3

我正在尝试使用 .xml 提取 XML 文档的两个标签之间的文本值xml.etree.ElementTree。在以下示例中,这将是值text twotext three。我只能提取text one. 我如何从<c>标签中找到其他文本?

import xml.etree.ElementTree as ET

root = ET.fromstring(
"<foo><c>text one<sub>ttt</sub>text two<sub>uuu</sub>text three</c></foo>")

print root[0].text # text one
4

1 回答 1

4

使用itertext

>>> z
<Element 'c' at 0x1030697d0>
>>> for i in z.itertext():
...   print(i)
...
text one
ttt
text two
uuu
text three
于 2014-08-09T12:20:21.040 回答