目前我正在研究一个语料库/数据集。它是 xml 格式,如下图所示。我面临一个问题。我想一一访问所有'ne'元素,如下图所示。然后我想访问'ne'元素内的'W'元素的文本。然后我想将你的符号'SDi'和'EDi'与这些'W'元素的文本连接起来。'i' 可以取从 1 开始的任何正整数。在 'SDi' 的情况下,我只需要在 'ne' 元素内的第一个 'W' 元素的文本。在“EDi”的情况下,我只需要最后一个“W”元素的文本那是在'ne'元素内。目前我在运行代码后没有得到任何输出。我认为这是因为元素“W”从未被访问过。此外,我认为元素'W'未被访问,因为它是元素'ne'的孙子,因此它不能直接访问,而是在其父节点的帮助下可能是可能的。
注1:“ne”元素中子元素的个数和名称不相同。
注2:这里只说明需要的东西。您可能会在编码/图片中找到一些其他细节,但忽略它们。
我正在使用 Spyder (python 3.6) 任何帮助将不胜感激。
我正在处理的 XML 文件中的图片如下所示:
XML文件的文本版本: 点击这里
示例/预期输出图像(下):
到目前为止我所做的编码:
for i in range(len(List_of_root_nodes)):
true_false = True
current = List_of_root_nodes[i]
start_ID = current.PDante_ID
#print('start:', start_ID) # For Testing
end_ID = None
number = str(i+1) # This number will serve as i used with SD and ED that is (SDi and EDi)
discourse_starting_symbol = "SD" + number
discourse_ending_symbol = "ED" + number
while true_false:
if current.right_child is None:
end_ID = current.PDante_ID
#print('end:', end_ID) # For Testing
true_false = False
else:
current = current.right_child
# Finding 'ne' element with id='start_ID'
ne_text = None
ne_id = None
for ne in myroot.iter('ne'):
ne_id = ne.get('id')
# If ne_id matches with start_ID means the place where SDi is to be placed is found
if ne_id == start_ID:
for w in ne.iter('W'):
ne_text = str(w.text)
boundary_and_text = " " + str(discourse_starting_symbol) + " " + ne_text
w.text = boundary_and_text
break
慕标5832272
元芳怎么了
相关分类