1

我有以下格式的 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
4

3 回答 3

0

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

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

您可能会观察到单词的顺序不是您想要的,但您可以通过记住同时具有尾部和文本的项目并在该迭代之后插入尾部来解决这个问题。不确定这是正确的方法。

于 2020-04-21T16:27:17.060 回答
0

我认为您正在寻找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
于 2020-04-21T17:23:24.460 回答
0

另一种解决方案。

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

于 2020-04-22T10:03:06.357 回答