text extraction using python lxml looping issue

只谈情不闲聊 提交于 2019-12-11 06:27:32

问题


Here is a part of my xml file..

- <a:p>
    - <a:pPr lvl="2">
        - <a:spcBef>
              <a:spcPts val="200" /> 
          </a:spcBef>
     </a:pPr>
    - <a:r>
          <a:rPr lang="en-US" sz="1400" dirty="0" smtClean="0" /> 
          <a:t>The</a:t> 
     </a:r>
    - <a:r>
         <a:rPr lang="en-US" sz="1400" dirty="0" /> 
         <a:t>world</a:t> 
      </a:r>
     - <a:r>
          <a:rPr lang="en-US" sz="1400" dirty="0" smtClean="0" /> 
          <a:t>is small</a:t> 
      </a:r>
  </a:p>
    - <a:p>
    - <a:pPr lvl="2">
        - <a:spcBef>
              <a:spcPts val="200" /> 
          </a:spcBef>
     </a:pPr>
    - <a:r>
          <a:rPr lang="en-US" sz="1400" dirty="0" smtClean="0" b="0" /> 
          <a:t>The</a:t> 
     </a:r>
    - <a:r>
         <a:rPr lang="en-US" sz="1400" dirty="0" b="0" /> 
         <a:t>world</a:t> 
      </a:r>
     - <a:r>
          <a:rPr lang="en-US" sz="1400" dirty="0" smtClean="0" b="0" /> 
          <a:t>is too big</a:t> 
      </a:r>
  </a:p>

I have written a code using lxml to extract the text. But, as the sentence is split into two lines, I want to join these two to form a single sentence like The world is small... . So here I write a code:

path4 = file.xpath('/p:sld/p:cSld/p:spTree/p:sp/p:txBody/a:p/a:r/a:rPr', namespaces={'p':'http://schemas.openxmlformats.org/presentationml/2006/main',
                'a':'http://schemas.openxmlformats.org/drawingml/2006/main'})
    if path5:
        for a in path4:  
            if a.get('sz') == '1400' and a.xpath('node()') == [] and a.get('b') != '0':
                b = a.getparent()
                c = b.getparent()
                d = c.xpath('./a:r/a:t/text()' , namespaces {'p':'http://schemas.openxmlformats.org/presentationml/2006/main', 'a':'http://schemas.openxmlformats.org/drawingml/2006/main'})
                print ''.join(d)
             elif a.get('sz') == '1400' and a.xpath('node()') == [] and a.get('b') == '0':
                b = a.getparent()
                c = b.getparent()
                d = c.xpath('./a:r/a:t/text()' , namespaces {'p':'http://schemas.openxmlformats.org/presentationml/2006/main', 'a':'http://schemas.openxmlformats.org/drawingml/2006/main'})
                print ''.join(d)

I get the output :

The world is samll...
The world is small...
The world is small...

expected output:

the world is small...

any suggestions?


回答1:


You are making the sentence for every a:rPr found in the loop.

Here's an example of what you should do instead:

test.xml:

<body xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"
      xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main">
    <a:p>
        -
        <a:pPr lvl="2">
            -
            <a:spcBef>
                <a:spcPts val="200"/>
            </a:spcBef>
        </a:pPr>
        -
        <a:r>
            <a:rPr lang="en-US" sz="1400" dirty="0" smtClean="0"/>
            <a:t>The</a:t>
        </a:r>
        -
        <a:r>
            <a:rPr lang="en-US" sz="1400" dirty="0"/>
            <a:t>world</a:t>
        </a:r>
        -
        <a:r>
            <a:rPr lang="en-US" sz="1400" dirty="0" smtClean="0"/>
            <a:t>is small</a:t>
        </a:r>
    </a:p>
    <a:p>
        -
        <a:pPr lvl="2">
            -
            <a:spcBef>
                <a:spcPts val="200"/>
            </a:spcBef>
        </a:pPr>
        -
        <a:r>
            <a:rPr lang="en-US" sz="1400" dirty="0" smtClean="0" b="0"/>
            <a:t>The</a:t>
        </a:r>
        -
        <a:r>
            <a:rPr lang="en-US" sz="1400" dirty="0" b="0"/>
            <a:t>world</a:t>
        </a:r>
        -
        <a:r>
            <a:rPr lang="en-US" sz="1400" dirty="0" smtClean="0" b="0"/>
            <a:t>is too big</a:t>
        </a:r>
    </a:p>
</body>

test.py:

from lxml import etree


tree = etree.parse('test.xml')
NAMESPACES = {'p': 'http://schemas.openxmlformats.org/presentationml/2006/main',
              'a': 'http://schemas.openxmlformats.org/drawingml/2006/main'}

path = tree.xpath('/body/a:p', namespaces=NAMESPACES)

for outer_item in path:
    parts = []
    for item in outer_item.xpath('./a:r/a:rPr', namespaces=NAMESPACES):
        parts.append(item.getparent().xpath('./a:t/text()', namespaces=NAMESPACES)[0])

    print " ".join(parts)

output:

The world is small

The world is too big

So, just looping over a:p items and extracting the text into parts, then print it after processing of each a:p. I've removed if statement for clarity.

Hope that helps.



来源:https://stackoverflow.com/questions/16938112/text-extraction-using-python-lxml-looping-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!