Python Xml中的单词合并

Python Xml中的单词合并,python,xml,python-2.7,lxml,findall,Python,Xml,Python 2.7,Lxml,Findall,在以下xml中: <w:body> <w:p w:rsidR="00912B30" w:rsidRPr="00912B30" w:rsidRDefault="00912B30" w:rsidP="00912B30"> <w:pPr> <w:autoSpaceDE w:val="0"/> <w:autoSpaceDN w:val="0"/> &

在以下xml中:

<w:body>
    <w:p w:rsidR="00912B30" w:rsidRPr="00912B30" w:rsidRDefault="00912B30" w:rsidP="00912B30">
        <w:pPr>
            <w:autoSpaceDE w:val="0"/>
            <w:autoSpaceDN w:val="0"/>
            <w:rPr>
                <w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman"/>
                <w:snapToGrid w:val="0"/>
                <w:kern w:val="0"/>
                <w:szCs w:val="21"/>
            </w:rPr>
        </w:pPr>
        <w:r w:rsidRPr="00912B30">
            <w:rPr>
                <w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman"/>
                <w:snapToGrid w:val="0"/>
                <w:kern w:val="0"/>
                <w:szCs w:val="21"/>
            </w:rPr>
            <w:t xml:space="preserve">Considering those situations, after 1970 The </w:t>
        </w:r>
        <w:r w:rsidRPr="00E155EC">
            <w:rPr>
                <w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman"/>
                <w:strike/>
                <w:snapToGrid w:val="0"/>
                <w:kern w:val="0"/>
                <w:szCs w:val="21"/>
            </w:rPr>
            <w:t>Agricultural Land Law</w:t>
        </w:r>
        <w:r w:rsidRPr="00912B30">
            <w:rPr>
                <w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman"/>
                <w:snapToGrid w:val="0"/>
                <w:kern w:val="0"/>
                <w:szCs w:val="21"/>
            </w:rPr>
            <w:t xml:space="preserve"> of 1952 was modified and changed the principle to permit renting and lending agricultural land. The way of thinking was as follows. If it was difficult to widen farmers’ size by buying agricultural land, expanding the size by renting would be possible. After that some positive framework to promote renting and lending agricultural land. For example, The </w:t>
        </w:r>
        <w:r w:rsidRPr="00E155EC">
            <w:rPr>
                <w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman"/>
                <w:strike/>
                <w:snapToGrid w:val="0"/>
                <w:kern w:val="0"/>
                <w:szCs w:val="21"/>
            </w:rPr>
            <w:t>Agricultural Land Use Promotion Project</w:t>
        </w:r>
        <w:r w:rsidRPr="00912B30">
            <w:rPr>
                <w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman"/>
                <w:snapToGrid w:val="0"/>
                <w:kern w:val="0"/>
                <w:szCs w:val="21"/>
            </w:rPr>
            <w:t xml:space="preserve"> had started in 1975 and The </w:t>
        </w:r>
        <w:r w:rsidRPr="00E155EC">
            <w:rPr>
                <w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman"/>
                <w:strike/>
                <w:snapToGrid w:val="0"/>
                <w:kern w:val="0"/>
                <w:szCs w:val="21"/>
            </w:rPr>
            <w:t>Agricultural Land Use Promotion Law</w:t>
        </w:r>
        <w:r w:rsidRPr="00912B30">
            <w:rPr>
                <w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman"/>
                <w:snapToGrid w:val="0"/>
                <w:kern w:val="0"/>
                <w:szCs w:val="21"/>
            </w:rPr>
            <w:t xml:space="preserve"> was established in 1980. Actually after that, area of agricultural land by transfer of ownership of owned agricultural land with compensation had been more than the area by transfer of rights for </w:t>
        </w:r>
        <w:r w:rsidRPr="00912B30">
            <w:rPr>
                <w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman"/>
                <w:snapToGrid w:val="0"/>
                <w:kern w:val="0"/>
                <w:szCs w:val="21"/>
            </w:rPr>
            <w:lastRenderedPageBreak/>
            <w:t>lease.</w:t>
        </w:r>
    </w:p>
</w:body>  
输出:

 text = ""  #initialize empty string where all words will be stored
    source = etree.parse(doc_xml)
    for p in source.findall('.//'+w1+'p'): #iterate over every p tag
        text+= " "      # add a space to separate words in successive paragraphs
        for b in p.findall('.//{%(ns)s}strike/../..//{%(ns)s}t' %{'ns':w}):
            text+=''.join(b.text) #joins all strike text and appends to empty string
text =" Agricultural Land LawAgricultural Land Use Promotion ProjectAgricultural Land Use Promotion Law"
预期输出:

text = " Agricultural Land Law Agricultural Land Use Promotion Project Agricultural Land Use Promotion Law" 
原始修复: 将最后一行代码替换为:

text+=" " +''.join(b.text)
它修复了上述问题,但在许多情况下,一个单词有两个罢工实例,因此原始修复可能会输出
“he lp”
,而不是
“help”
。这有点棘手,我想到了:
1.提取罢工文本
2.检查下一个文本标记。如果它没有罢工标记,添加一个空格的文字,否则如果它有罢工标记,直接加入它

下面是一个单独实例中出现的单词示例:

<w:r w:rsidRPr:00C42D65>
   <w:rpr>
     <w:strike>
   <w:t>(IQR
<w:r w:rsidRPr:00C42D65>
  <w:rpr>
     <w:strike>
  <w:t>)

问题似乎出在if语句xpath表达式中。

使用
xpath
名称空间,并使用
''连接结果(字符串列表)。连接(…)

更新

text = ''
for t in source.xpath('.//w:p//w:r//w:t',namespaces={'w': w}):
    if t.xpath('..//w:strike',namespaces={'w': w}):
        text += t.text
    else:
        if text:  # To prevent space before the first text.
            text += ' '

为什么不将它们添加到一个列表中,而不是一个连续的字符串中,然后您可以关注如何组合它们。如果没有一个将一个单词拆分为多个标记的示例,那么很难对这些情况提出合理的建议。我将添加一个将单词拆分为多个标记的示例。示例添加了@jornsharpe,这是一小段xml@jonrsharpe你知道我添加的更新有什么问题吗?它与text+=''++''相同。join(b.text)@Swordy,他们是不同的<代码>''.join('text')=='text'
@Swordy,它类似于
text+=''+b.text
内部循环。(除第一个元素外)是否有一个“not equal”函数来匹配某个未出现的属性??就像这里的一个:@Swordy,是的,有:
//element[not(@attribute you not want)]
。对于其他,请搜索
xpath
...

source = etree.parse(doc_xml)
text = ' '.join(
    source.xpath('.//w:p//w:strike/../..//w:t/text()', namespaces={'w': w})
)
text = ''
for t in source.xpath('.//w:p//w:r//w:t',namespaces={'w': w}):
    if t.xpath('..//w:strike',namespaces={'w': w}):
        text += t.text
    else:
        if text:  # To prevent space before the first text.
            text += ' '