python 在 elementtree 中使用 SimpleXMLTreeBuilder
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1068510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using SimpleXMLTreeBuilder in elementtree
提问by Shard
I have been developing an application with django and elementtree and while deploying it to the production server i have found out it is running python 2.4. I have been able to bundle elementtree but now i am getting the error:
我一直在用 django 和 elementtree 开发一个应用程序,在将它部署到生产服务器时,我发现它运行的是 python 2.4。我已经能够捆绑 elementtree,但现在出现错误:
"No module named expat; use SimpleXMLTreeBuilder instead"
Unfortunately i cannot upgrade python so im stuck with what i got. How do i use SimpleXMLTreeBuilder as the parser and/or will i need to rewrite code?
不幸的是我无法升级 python 所以我坚持我得到的东西。我如何使用 SimpleXMLTreeBuilder 作为解析器和/或我是否需要重写代码?
回答by thom_nic
If you have third party module that wants to use ElementTree (and XMLTreeBuilder by dependency) you can change ElementTree's XMLTreeBuilder definition to the one provided by SimpleXMLTreeBuilder like so:
如果您有第三方模块想要使用 ElementTree(以及依赖关系的 XMLTreeBuilder),您可以将 ElementTree 的 XMLTreeBuilder 定义更改为 SimpleXMLTreeBuilder 提供的定义,如下所示:
from xml.etree import ElementTree # part of python distribution
from elementtree import SimpleXMLTreeBuilder # part of your codebase
ElementTree.XMLTreeBuilder = SimpleXMLTreeBuilder.TreeBuilder
Now ElementTree will always use the SimpleXMLTreeBuilder whenever it's called.
现在,每当调用 ElementTree 时,它都将始终使用 SimpleXMLTreeBuilder。
See also: http://groups.google.com/group/google-appengine/browse_thread/thread/b7399a91c9525c97
另请参阅:http: //groups.google.com/group/google-appengine/browse_thread/thread/b7399a91c9525c97
回答by Ben
We ran into this same problem using python version 2.6.4 on CentOS 5.5.
我们在 CentOS 5.5 上使用 python 版本 2.6.4 遇到了同样的问题。
The issue happens when the expat class attempts to load the pyexpat modules, see /usr/lib64/python2.6/xml/parsers/expat.py
当 expat 类尝试加载 pyexpat 模块时会发生此问题,请参阅 /usr/lib64/python2.6/xml/parsers/expat.py
Looking inside of /usr/lib64/python2.6/lib-dynload/, I didn't see the "pyexpat.so" shared object. However, I didsee it on another machine, which wasn't having the problem.
查看 /usr/lib64/python2.6/lib-dynload/ 内部,我没有看到“pyexpat.so”共享对象。但是,我确实在另一台没有问题的机器上看到了它。
I compared the python versions (yum list 'python*') and identified that the properly functioning machine had python 2.6.5. Running 'yum update python26' fixed the issue for me.
我比较了 python 版本(yum list 'python*')并确定运行正常的机器有 python 2.6.5。运行 'yum update python26' 为我解决了这个问题。
If that doesn't work for you and you want a slapstick solution, you can copy the SO file into your dynamic load path.
如果这对您不起作用并且您想要一个闹剧解决方案,您可以将 SO 文件复制到您的动态加载路径中。
回答by nevesly
I have same error as you when I developed on Solaris & python 2.7.2.
I fixed this issue after I installed the python expat
package by using pkgin.
See if the solution above can give you any idea.
当我在 Solaris & python 2.7.2 上开发时,我和你有同样的错误。我在expat
使用 pkgin安装 python包后修复了这个问题。看看上面的解决方案是否可以给你任何想法。
回答by Alex Martelli
Assuming you're now using elementtree.XMLTreeBuilder
, just try this instead:
假设您现在正在使用elementtree.XMLTreeBuilder
,请尝试以下操作:
from elementtree import SimpleXMLTreeBuilder as XMLTreeBuilder
It tries to provide exactly the same functionality but using xmllib instead of expat. If that also fails, BTW, try:
它试图提供完全相同的功能,但使用 xmllib 而不是 expat。如果这也失败了,顺便说一句,请尝试:
from elementtree import SgmlopXMLTreeBuilder as XMLTreeBuilder
to attempt to use yet another implementation, this one based on sgmlop instead.
尝试使用另一种实现,该实现基于 sgmlop。
回答by Alex Martelli
from elementtree import SimpleXMLTreeBuilder as XMLTreeBuilder
从 elementtree 导入 SimpleXMLTreeBuilder 作为 XMLTreeBuilder
Ok, it has slightly changed now:
好的,它现在略有变化:
Traceback (most recent call last):
File "C:\Python26\tests\xml.py", line 12, in <module>
doc = elementtree.ElementTree.parse("foo.xml")
File "C:\Python26\lib\site-packages\elementtree\ElementTree.py", line 908, in parse
tree = parser_api.parse(source)
File "C:\Python26\lib\site-packages\elementtree\ElementTree.py", line 169, in parse
parser = XMLTreeBuilder()
File "C:\Python26\lib\site-packages\elementtree\ElementTree.py", line 1165, in __init__
"No module named expat; use SimpleXMLTreeBuilder instead"
ImportError: No module named expat; use SimpleXMLTreeBuilder instead
I think it was statically linked with older version of Python or something. Is there an easy way to attach XML parser to Python 2.6? Some libraries seem to only work with older versions 8(
我认为它与旧版本的 Python 或其他东西静态链接。有没有一种简单的方法可以将 XML 解析器附加到 Python 2.6?有些库似乎只适用于旧版本 8(