[英]Extracting text from an xml doc with Python ElementTree
我有一个 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>
我想提取所有文本,无论是未封装在注释标签中的文本,还是注释标签中的文本,以重建原始短语 So My output 将是 --> 记得录制电影出租车司机
问题显然是没有办法获得令牌'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
你非常接近。 我会这样做:
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
您可能会观察到单词的顺序不是您想要的,但您可以通过记住同时具有尾部和文本的项目并在该迭代之后插入尾部来解决这个问题。 不确定这是正确的方法。
我认为您正在寻找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
另一种解决方案。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.