我仍然是编程新手,但我了解一些Python,并且对XPath和XML总体上很熟悉。目前,我正在处理一些看起来像这样的XML数据:
<foo>
<bar>
<unit>
<structure>
<token word="Rocky" att1="noun" att2="name">Rocky</token>
<token word="the" att1="article" att2="">the</token>
<token word="yellow" att1="adjective" att2="color">yellow</token>
<token word="dog" att1="noun" att2="animal">dog</token>
</structure>
</unit>
</bar>
</foo>
现在我需要做的是首先找到一个属性值,让我们
<token word="dog" att1="noun"att2="animal"</token>
对于一个实例。因此,在文档的所有结构中,我首先要查找所有具有动物作为att2值的节点,然后将该节点的所有兄弟节点放入列表中。因为节点每个都有几个属性,所以我试图将它们每个都包含到一个不同的列表中,也就是说,从结构中的所有属性中创建一个列表,该结构中的动物具有其子代的att2值之一。例如:
listWord = [Rocky, the, yellow, dog]
listAtt1 = [noun, article, adjective, noun]
listAtt2 = [name, ,color, animal]
目前,我只是想知道是否有可能。到目前为止,我只靠属性结构碰壁,更不用说空值了。
包括结束标记标记,并假设您的文本包含在中test.xml
,以下内容:
import xml.etree.ElementTree
e = xml.etree.ElementTree.parse('test.xml').getroot()
listWord = []
listAtt1 = []
listAtt2 = []
for child in e.iter('token'):
listWord.append(child.attrib['word'])
listAtt1.append(child.attrib['att1'])
listAtt2.append(child.attrib['att2'])
print listWord
print listAtt1
print listAtt2
将返回:
['Rocky', 'the', 'yellow', 'dog']
['noun', 'article', 'adjective', 'noun']
['name', '', 'color', 'animal']
e.iter()
让您e
作为根及其下的元素进行迭代-我们指定的标记token
仅返回token
元素。child.attrib
返回属性字典,我们将其附加到列表中。
编辑:对于您的问题的第二点,我认为以下内容(虽然可能不是最佳实践)会满足您的需求:
import xml.etree.ElementTree
e = xml.etree.ElementTree.parse('test.xml').getroot()
listWord = []
listAtt1 = []
listAtt2 = []
animal_structs =[]
for structure in e.iter('structure'):
for child in structure.iter('token'):
if 'att2' in child.keys():
if child.attrib['att2'] == 'animal':
animal_structs.append(structure)
break
for structure in animal_structs:
for child in structure.iter('token'):
listWord.append(child.attrib['word'])
listAtt1.append(child.attrib['att1'])
listAtt2.append(child.attrib['att2'])
print listWord
print listAtt1
print listAtt2
我们首先创建一个structure
带有animal
子元素的所有元素的列表,然后返回每个结构的所有then属性。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句