Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 出现在单独节点中的句子单词,Elementtree_Python_Xml_Python 2.7_Docx_Elementtree - Fatal编程技术网

Python 出现在单独节点中的句子单词,Elementtree

Python 出现在单独节点中的句子单词,Elementtree,python,xml,python-2.7,docx,elementtree,Python,Xml,Python 2.7,Docx,Elementtree,基本上,我创建了一个word文档来检查XML中的解析是如何进行的。我做到了: import xml.etree.ElementTree import zipfile as zf z = zf.ZipFile("INTRODUCTION.docx") doc_xml = z.open("word/document.xml") tree = ET.parse(doc_xml) NAMESPACE_PREFIXES = { 'w': 'http://schemas.open

基本上,我创建了一个word文档来检查XML中的解析是如何进行的。我做到了:

import xml.etree.ElementTree  
import zipfile as zf  
z = zf.ZipFile("INTRODUCTION.docx")  
doc_xml = z.open("word/document.xml")  

tree = ET.parse(doc_xml)

NAMESPACE_PREFIXES = {
    'w': 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'
    }  


text_elements = [element for element in tree.iter() if element.tag == 
'{' + NAMESPACE_PREFIXES['w'] + '}t']
for node in text_elements:
    print node.text  
名称空间前缀负责处理这些链接,以便忽略它们。node.text已打印为:

INTRODUCTION
This is a test document for xml
.
Lets
 see how this works.
Conclusion
It should hopefully
..
在我的原始文档中,
让我们看看它是如何工作的
出现在一行中,类似地,我看到相同句子的句号出现在不同的节点中(如“.”)。我如何解决它?以下是xml代码:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\r\n
<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 wp14">
    <w:body>
        <w:p w:rsidR="00470EEF" w:rsidRDefault="00456755"><w:pPr><w:rPr><w:b/></w:rPr></w:pPr><w:r w:rsidRPr="00456755"><w:rPr><w:b/></w:rPr><w:t>INTRODUCTION</w:t></w:r></w:p>
        <w:p w:rsidR="00456755" w:rsidRDefault="00456755"><w:r w:rsidRPr="00456755"><w:t>This is a test document for xml</w:t></w:r><w:r><w:t>.</w:t></w:r></w:p>
        <w:p w:rsidR="00456755" w:rsidRDefault="00456755"><w:proofErr w:type="spellStart"/><w:proofErr w:type="gramStart"/><w:r><w:t>Lets</w:t></w:r><w:proofErr w:type="spellEnd"/><w:proofErr w:type="gramEnd"/><w:r><w:t xml:space="preserve"> see how this works.</w:t></w:r></w:p>
        <w:p w:rsidR="00456755" w:rsidRDefault="00456755"/>
        <w:p w:rsidR="00456755" w:rsidRDefault="00456755"/>
        <w:p w:rsidR="00456755" w:rsidRDefault="00456755"><w:pPr><w:rPr><w:b/></w:rPr></w:pPr><w:r w:rsidRPr="00456755"><w:rPr><w:b/></w:rPr><w:t>Conclusion</w:t></w:r></w:p>
        <w:p w:rsidR="00456755" w:rsidRPr="00456755" w:rsidRDefault="00456755"><w:r w:rsidRPr="00456755"><w:t>It should hopefully</w:t></w:r><w:r><w:t>..</w:t></w:r><w:bookmarkStart w:id="0" w:name="_GoBack"/><w:bookmarkEnd w:id="0"/></w:p>
        <w:sectPr w:rsidR="00456755" w:rsidRPr="00456755"><w:pgSz w:w="11906" w:h="16838"/><w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="708" w:footer="708" w:gutter="0"/><w:cols w:space="708"/><w:docGrid w:linePitch="360"/></w:sectPr>
    </w:body>
</w:document>
\r\n
介绍
这是一个xml测试文档。
让我们看看它是如何工作的。
结论
我希望。。

我注意到类似于
w:type=“spellStart”
“grasmStart”
的内容,这就是为什么
出现在不同节点中的原因。是否有办法查看此内容?

print
语句在打印的字符串后添加换行符

您需要按
p
标记对标记进行分组:查找
p
标记,并在标记中查找
t
标记

...
w = 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'    
for p in tree.findall('.//{' + w + '}p'):
    print ''.join(t.text for t in p.findall('.//{' + w + '}t'))
输出:

INTRODUCTION
This is a test document for xml.
Lets see how this works.


Conclusion
It should hopefully..

谢谢,你能解释一下到底发生了什么以及这是如何解决的吗?@Swordy,我想我在编辑答案(添加解释),而你在评论。请再次检查答案。@Swordy,对于传递给
findall
的表达式,请参阅。我们是否可以添加文本的条件w.r.t属性,如颜色或字体类型?@Swordy,如果您遵循我在前面的评论中提供的链接,您将发现类似
[@attrib='value']
的XPath语法。如果您需要更复杂的条件,那么最好查看支持更多XPath语法的条件。