- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在将 word 文档转换为 xml 以使用以下代码比较它们:
word = win32com.client.Dispatch('Word.Application')
wd = word.Documents.Open(inFile)
# Converts the word infile to xml outfile
wd.SaveAs(outFile,11)
wd.Close()
dom=parse(outFile)
我得到的 xml 文件如下所示:
<?xml version="1.0" encoding="utf-8"?>
<?mso-application progid="Word.Document"?>
<w:wordDocument w:embeddedObjPresent="no" w:macrosPresent="no" w:ocxPresent="no" xml:space="preserve" xmlns:aml="http://schemas.microsoft.com/aml/2001/core" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:sl="http://schemas.microsoft.com/schemaLibrary/2003/core" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:wsp="http://schemas.microsoft.com/office/word/2003/wordml/sp2" xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint">
<w:ignoreSubtree w:val="http://schemas.microsoft.com/office/word/2003/wordml/sp2"/>
<w:shapeDefaults>
<o:shapedefaults spidmax="1027" v:ext="edit"/>
<o:shapelayout v:ext="edit">
<o:idmap data="1" v:ext="edit"/>
</o:shapelayout>
</w:shapeDefaults>
<w:body>
<wx:sect>
<w:tbl>
<w:tblGrid>
<w:gridCol w:w="200"/>
...
</w:tblGrid>
<w:pict>
<v:shapetype coordsize="21600,21600" filled="f" id="_x0000_t75" o:preferrelative="t" o:spt="75" path="m@4@5l@4@11@9@11@9@5xe" stroked="f">
<v:stroke joinstyle="miter"/>
<v:formulas>
<v:f eqn="if lineDrawn pixelLineWidth 0"/>
...
</v:formulas>
<v:path gradientshapeok="t" o:connecttype="rect" o:extrusionok="f"/>
<o:lock aspectratio="t" v:ext="edit"/>
</v:shapetype>
<v:shape id="Picture" o:spid="_x0000_s1026" style="position:absolute;left:0;text-align:left;margin-left:0;margin-top:0;width:400pt;height:40pt;z-index:1;visibility:visible;mso-wrap-style:square;mso-wrap-distance-left:0;mso-wrap-distance-top:0;mso-wrap-distance-right:0;mso-wrap-distance-bottom:0;mso-position-horizontal:left;mso-position-horizontal-relative:text;mso-position-vertical:absolute;mso-position-vertical-relative:line" type="#_x0000_t75">
<v:imagedata o:title="" src="wordml://03000001.png"/>
<w10:wrap anchory="line"/>
<w10:anchorlock/>
</v:shape>
</w:pict>
...
我不能使用 xpath 函数(lxml 库)例如,当我尝试时:
import lxml.etree as et
tree = et.parse(xmlFile)
for elt in tree.xpath("//w:gridCol"):
elt.getparent().remove(elt)
我收到以下错误:
for elt in tree.xpath("//w:gridCol"):
File "lxml.etree.pyx", line 2029, in lxml.etree._ElementTree.xpath (src/lxml/lxml.etree.c:45934)
File "xpath.pxi", line 379, in lxml.etree.XPathDocumentEvaluator.__call__ (src/lxml/lxml.etree.c:114389)
File "xpath.pxi", line 242, in lxml.etree._XPathEvaluatorBase._handle_result (src/lxml/lxml.etree.c:113063)
File "xpath.pxi", line 227, in lxml.etree._XPathEvaluatorBase._raise_eval_error (src/lxml/lxml.etree.c:112894)
XPathEvalError: Undefined namespace prefix
我做了一些研究,我猜这是一个命名空间问题,但我不知道如何解决它?
最佳答案
在这段代码中:
for elt in tree.xpath("//w:gridCol"):
w:
不是命名空间;它是一个命名空间 prefix ,它是实际命名空间的有效简写,http://schemas.microsoft.com/office/word/2003/wordml
。如果要使用 xpath
方法搜索此命名空间中的元素,则需要为其提供命名空间前缀到命名空间的映射:
tree.xpath("//w:gridCol", namespaces={
'w': 'http://schemas.microsoft.com/office/word/2003/wordml',
})
另请注意,不要求您使用相同的命名空间前缀。以下将找到相同的元素:
tree.xpath("//bob:gridCol", namespaces={
'bob': 'http://schemas.microsoft.com/office/word/2003/wordml'
})
关于python - 命名空间错误lxml xpath python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24038744/
我有以下xml文件 10.10.0.135::3111 10.10.0.130::3111 10.10.0.129::3111 10.
我已经为 PyPy 创建了一个 virtualenv: virtualenv test -p `which pypy` source test/bin/activate 我安装了以下依赖项: sudo
我正在尝试使用 lxml 的 ElementTree etree 在我的 xml 文档中查找特定标签。标签如下所示: 12 我希望使用 etree.find('text:statedAge
在 python 3.5 项目中,我必须读取一些 xml 文件,因此我决定使用 lxml 库。由于我正在阅读文件,因此根据文档执行此操作的最有效方法是使用 lxml.etree.parse(...)
执行 pip install lxml 或 pip install pyquery 会出现此错误: gcc: error trying to exec 'cc1': execvp: No such f
我正在使用下面的代码获取一个部分的所有 html 内容以保存到数据库 el = doc.get_element_by_id('productDescription') lxml.html.tostri
我想以可区分的方式打印出 etree 的树结构(由 html 文档形成)(意味着两个 etree 应该以不同的方式打印出来)。 我所说的结构是指树的“形状”,基本上是指所有标签,但没有属性,也没有文本
谁能解释为什么第一次调用 root.cssselect() 有效,而第二次失败? from lxml.html import fromstring from lxml import etree htm
我收到此错误“ImportError: No module named lxml”,即使确实安装了 LXML。具体来说,它安装在项目的 python Virtualenv 中。最终我正在研究 Pyth
我在 Windows 32 位上使用 Anaconda v4.2 和 Python 3.5,并想使用 lxml etree。我的 Anaconda 发行版包括 lxml 3.6.4,但我的 IDE(P
我有一个脚本,可以从 URL 列表的 XML 文件中提取一些术语。所有 URL 都可以访问 XML 数据。 它在第一次正确打开、解析和提取时工作正常,但随后在过程中被某些 XML 文件中断并出现此错误
我正在使用 mechanize/cookiejar/lxml 来读取页面,它适用于某些页面但不适用于其他页面。我在其中遇到的错误是标题中的错误。我不能在这里发布页面,因为它们不是 SFW,但是有没有办
这是一个基本的问题,我实际上在文档中找不到它:-/ 如下: img = house_tree.xpath('//img[@id="mainphoto"]')[0] 如何获取 的 HTML标记? 我尝
我只想说,这个问题我已经在Pip is already installed: but I am getting no module named lxml看到了并且已经看到关于以非 root 用户身份安
我通过 pip 安装了 lxml 3.3.5。现在我在运行一些 Django 测试时遇到了问题: Traceback (most recent call last): File "manage.p
我有一个 Django 应用程序,其中包含 Tastypie 提供的 RESTFull 服务。我使用 easy_install 安装 lxml 和 defusedxml,这样我就可以使用 xml 而不
我正在使用 lxml etree 创建 xml 或 REST 调用。我有命名空间的问题,因为如果没有正确制定,我会从服务器收到语法错误。 正如您在以下 2 个示例中所看到的,我应该得到例如 ns1、n
我对导航具有 lxml.etree namespace 的 xml 文档感到有些困惑。 .我已经看到了一些关于这个主题的主题( 1 、 2 )以及 lxml docs但仍然没有想出答案。 xml =
由于各种原因,我试图从lxml.html.fromstring()切换到lxml.html.html5parser.document_fromstring()。两者之间的最大区别是,第一个返回lxml
我需要通过向现有元素添加子元素来修改现有的 xml 文件。我使用 lxml 库。 Eric Idle 999-999-999 555-555-555
我是一名优秀的程序员,十分优秀!