【发布时间】:2019-05-10 21:50:34
【问题描述】:
我在 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 端,如sentence, txt, annotation,但排序没有效果。此外,我提供的 python 代码在 xml 文件中随机插入(老实说我不知道这是否有助于解决这个问题)字符串,如&#948;&#951;&#956;&#953;&#959;&#965;&#961;&#947;&#943;&#945;。
所以,我坚信问题出在嵌套标签上,但我无法弄清楚。有什么想法吗?
【问题讨论】:
-
你能用 lxml 代替 ElementTree 吗?我认为使用 xpath 会容易得多。
-
没想到,我去试试!
-
我会继续添加一个 lxml 答案;如果你需要一个例子。
标签: python xml lxml elementtree