使用Python ElementTree从xml文档中提取文本



我有一个xml文档,格式如下

<samples>
<sample count="10" intentref="none">
Remember to
<annotation conceptref="cf1">
<annotation conceptref="cf2">record</annotation>
</annotation>
the
<annotation conceptref="cf3">movie</annotation>
<annotation conceptref="cf4">Taxi driver</annotation>
</sample>
</samples>

我想提取所有的文本,要么是注释标签中没有封装的,要么是标注标签中的,以便重建原始短语所以我的输出是-->记住录制电影《出租车司机》

问题显然是无法获得代币"The"这里是我的代码的片段

import xml.etree.ElementTree as ET 
samples = ET.fromstring("""
<samples>
<sample count="10" intentref="none">Remember to<annotation conceptref="cf1"><annotation conceptref="cf2">record</annotation></annotation>the<annotation conceptref="cf3">movie</annotation><annotation conceptref="cf4">Taxi driver</annotation></sample>
</samples>
""")
for sample in samples.iter("sample"):
print ('***'+sample.text+'***'+sample.tail)
for annotation in sample.iter('annotation'):
print(annotation.text)
for nested_annotation in annotation.getchildren():
print(nested_annotation.text)

我以为嵌套注释会成功的。。但是没有,这是的结果

***Remember to'***
None
record
record
movie
Taxi driver

我想您正在寻找itertext方法:

# Iterate over all the sample block
for sample in tree.xpath('//sample'):
print(''.join(sample.itertext()))

完整代码:

# Load module
import lxml.etree as etree
# Load data
parser = etree.XMLParser(remove_blank_text=True)
tree = etree.parse('data.xml', parser)
# Iterate over all the sample block
for sample in tree.xpath('//sample'):
print(''.join(sample.itertext()))
# programmer l'
# enregistreur
# des
# oeuvres
# La Chevauchée de Virginia

你非常接近。我会这样做:

import xml.etree.ElementTree as ET

samples = ET.fromstring("""<samples>
<sample count="10" intentref="none">
Remember to
<annotation conceptref="cf1">
<annotation conceptref="cf2">record</annotation>
</annotation>
the
<annotation conceptref="cf3">movie</annotation>
<annotation conceptref="cf4">Taxi driver</annotation>
</sample>
</samples>
""")

for page in samples.findall('.//'):
text = page.text if page.text else ''
tail = page.tail if page.tail else ''
print(text + tail)

这将给你:


Remember to


the
record
movie
Taxi driver

你可能会发现单词的顺序不是你想要的,但你可能可以通过记住同时有尾部和文本的项目并在迭代后插入尾部来解决这个问题。不确定这是正确的方式强硬。

另一个解决方案。

from simplified_scrapy import SimplifiedDoc,req,utils
html = '''
<samples>
<sample count="10" intentref="none">
Remember to
<annotation conceptref="cf1">
<annotation conceptref="cf2">record</annotation>
</annotation>
the
<annotation conceptref="cf3">movie</annotation>
<annotation conceptref="cf4">Taxi driver</annotation>
</sample>
</samples>
'''
doc = SimplifiedDoc(html)
print(doc.selects('sample').text) # Extract all the text
# Another examples
for sample in doc.selects('sample'):
print (sample.count, sample.annotation.text)

结果:

['Remember to record the movie Taxi driver']
10 record

下面是更多的例子。https://github.com/yiyedata/simplified-scrapy-demo/tree/master/doc_examples

最新更新