Python 提取文本w.r.t属性值,lxml findall
我有以下xml:Python 提取文本w.r.t属性值,lxml findall,python,xml,python-2.7,lxml,findall,Python,Xml,Python 2.7,Lxml,Findall,我有以下xml: <?xml version="1.0" encoding="UTF-8"?> <w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:mc="http://schemas.openxmlformats.o
<?xml version="1.0" encoding="UTF-8"?>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" 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:v="urn:schemas-microsoft-com:vml" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 wp14">
<w:body>
<w:p w:rsidR="00DD23CE" w:rsidRPr="00D1434D" w:rsidRDefault="009764DB" w:rsidP="00660FF7">
<w:pPr>
<w:pStyle w:val="HTMLPreformatted" />
<w:spacing w:line="276" w:lineRule="auto" />
<w:jc w:val="both" />
<w:rPr>
<w:rFonts w:ascii="Times New Roman" w:eastAsia="MS PGothic" w:hAnsi="Times New Roman" />
<w:b />
<w:color w:val="000000" />
<w:sz w:val="24" />
<w:szCs w:val="24" />
<w:highlight w:val="yellow" />
</w:rPr>
</w:pPr>
<w:bookmarkStart w:id="0" w:name="_GoBack" />
<w:bookmarkEnd w:id="0" />
<w:r w:rsidRPr="00D1434D">
<w:rPr>
<w:rFonts w:ascii="Times New Roman" w:eastAsia="MS PGothic" w:hAnsi="Times New Roman" />
<w:b />
<w:color w:val="000000" />
<w:sz w:val="24" />
<w:szCs w:val="24" />
<w:highlight w:val="yellow" />
</w:rPr>
<w:t xml:space="preserve">Responses to </w:t>
</w:r>
<w:r w:rsidR="00335D4A" w:rsidRPr="00D1434D">
<w:rPr>
<w:rFonts w:ascii="Times New Roman" w:eastAsia="MS PGothic" w:hAnsi="Times New Roman" />
<w:b />
<w:color w:val="000000" />
<w:sz w:val="24" />
<w:szCs w:val="24" />
<w:highlight w:val="yellow" />
<w:lang w:eastAsia="ja-JP" />
</w:rPr>
<w:t>the Reviewer</w:t>
</w:r>
<w:r w:rsidR="00335D4A" w:rsidRPr="00D1434D">
<w:rPr>
<w:rFonts w:ascii="Times New Roman" w:eastAsia="MS PGothic" w:hAnsi="Times New Roman" />
<w:b />
<w:color w:val="000000" />
<w:sz w:val="24" />
<w:szCs w:val="24" />
<w:highlight w:val="yellow" />
</w:rPr>
<w:t xml:space="preserve">\xe2\x80\x99s </w:t>
</w:r>
<w:r w:rsidR="00DD23CE" w:rsidRPr="00D1434D">
<w:rPr>
<w:rFonts w:ascii="Times New Roman" w:eastAsia="MS PGothic" w:hAnsi="Times New Roman" />
<w:b />
<w:color w:val="000000" />
<w:sz w:val="24" />
<w:szCs w:val="24" />
<w:highlight w:val="yellow" />
</w:rPr>
<w:t>Comments</w:t>
</w:r>
</w:p>
<w:p w:rsidR="00200734" w:rsidRPr="00D1434D" w:rsidRDefault="00200734" w:rsidP="00660FF7">
<w:pPr>
<w:spacing w:line="276" w:lineRule="auto" />
<w:rPr>
<w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" />
<w:b />
<w:bCs />
<w:sz w:val="24" />
<w:szCs w:val="24" />
<w:highlight w:val="yellow" />
</w:rPr>
</w:pPr>
</w:p>
<w:p w:rsidR="00675BBA" w:rsidRPr="00D1434D" w:rsidRDefault="00675BBA" w:rsidP="00660FF7">
<w:pPr>
<w:spacing w:line="276" w:lineRule="auto" />
<w:rPr>
<w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" />
<w:b />
<w:bCs />
<w:sz w:val="24" />
<w:szCs w:val="24" />
<w:highlight w:val="yellow" />
</w:rPr>
</w:pPr>
<w:r w:rsidRPr="00D1434D">
<w:rPr>
<w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" />
<w:b />
<w:bCs />
<w:sz w:val="24" />
<w:szCs w:val="24" />
<w:highlight w:val="yellow" />
</w:rPr>
<w:t>Comments of Reviewer 1</w:t>
</w:r>
</w:p>
</w:body>
</w:document>
但当我指定属性值时,它不会返回任何内容:
w = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
for p in lxml_tree.findall('.//{' + w + '}p'):
for t in p.findall('.//{%(ns)s}highlight[@val="yellow"]/../..//{%(ns)s}t' %{'ns':w}):
print t.text
即使使用:
for p in lxml_tree.findall('.//{' + w + '}p'):
for t in p.findall('.//{%(ns)s}*[@highlight="yellow"]/../..//{%(ns)s}t' %{'ns':w}):
print t.text
不起作用
有什么问题吗?只需在val
前面添加{%(ns)s}
:
for p in lxml_tree.findall('.//{' + w + '}p'):
for t in p.findall('.//{%(ns)s}highlight[@{%(ns)s}val="yellow"]/../..//{%(ns)s}t' %{'ns':w}):
print t.text
我认为主要的问题不是给
val
attirbute命名。但您也可以使用内置的.xpath()
方法稍微简化代码,如下所示:
for t in lxml_tree.xpath('.//w:highlight[@w:val="yellow"]/../..//w:t',
namespaces={'w': "http://schemas.openxmlformats.org/wordprocessingml/2006/main",
}):
print t.text
for t in lxml_tree.xpath('.//w:highlight[@w:val="yellow"]/../..//w:t',
namespaces={'w': "http://schemas.openxmlformats.org/wordprocessingml/2006/main",
}):
print t.text