我在 python 中处理 XML 文件。我有一个包含多种语言的句子的数据集,其结构如下:
<corpus>
<sentence id="0">
<text lang="de">...</text>
<text lang="en">...</text>
<text lang="fr">...</text>
<!-- Other languages -->
<annotations>
<annotation lang="de">...</annotation>
<annotation lang="en">...</annotation>
<annotation lang="fr">...</annotation>
<!-- Other languages -->
</annotations>
</sentence>
<sentence id="1">
<!-- Other sentence -->
</sentence>
<!-- Other sentences -->
</corpus>
我想得到的是,从数据集开始,一个新的数据集只包含句子和英文注释(属性“lang”的“en”值)。我试过这个解决方案:
import xml.etree.ElementTree as ET
tree = ET.parse('samplefile2.xml')
root = tree.getroot()
for sentence in root:
if sentence.tag == 'sentence':
for txt in sentence:
if txt.tag == 'text':
if txt.attrib['lang'] != 'en':
sentence.remove(txt)
if txt.tag == 'annotations':
for annotation in txt:
if annotation.attrib['lang'] != 'en':
txt.remove(annotation)
tree.write('output.xml')
但是好像只在text
属性层面起作用,在annotation
属性层面不起作用。我什至尝试用增量索引 root[s]、root[s][t]、root[s][t ][a]
,但是排序没有效果。此外,我提供的 python 代码在 xml 文件中随机插入(老实说,我不知道这是否有助于解决这个问题)像 δημι 这样的字符串;ουργία
。
所以,我坚信问题出在嵌套标签中,但我无法弄清楚。一些想法?
最佳答案
如果您能够使用 lxml,我认为使用 xpath 会更容易...
XML 输入 (input.xml)
<corpus>
<sentence id="0">
<text lang="de">...</text>
<text lang="en">...</text>
<text lang="fr">...</text>
<!-- Other languages -->
<annotations>
<annotation lang="de">...</annotation>
<annotation lang="en">...</annotation>
<annotation lang="fr">...</annotation>
<!-- Other languages -->
</annotations>
</sentence>
<sentence id="1">
<!-- Other sentence -->
</sentence>
<!-- Other sentences -->
</corpus>
python
from lxml import etree
target_lang = "en"
tree = etree.parse("input.xml")
# Match any element that has a child that has a lang attribute with a value other than
# target_lang. We need this element so we can remove the child from it.
for parent in tree.xpath(f".//*[*[@lang != '{target_lang}']]"):
# Match the children that have a lang attribute with a value other than target_lang.
for child in parent.xpath(f"*[@lang != '{target_lang}']"):
# Remove the child from the parent.
parent.remove(child)
tree.write("output.xml")
XML 输出 (output.xml)
<corpus>
<sentence id="0">
<text lang="en">...</text>
<!-- Other languages -->
<annotations>
<annotation lang="en">...</annotation>
<!-- Other languages -->
</annotations>
</sentence>
<sentence id="1">
<!-- Other sentence -->
</sentence>
<!-- Other sentences -->
</corpus>
关于python - 没有在 python 中正确解析嵌套的 xml 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56085245/