问题描述
我刚刚完成了 Ubuntu 10.10 的全新安装,我正在尝试运行一些使用 xml 和 xpath 的脚本.我从 PyXML 内部收到错误消息.
I just completed a fresh install of Ubuntu 10.10 and I'm trying to run some scripts that use xml and xpath. I get an error from inside PyXML.
我认为这是一个安装错误.为了安装它,我执行了以下操作:
I think this is an install error. To get this installed I did the following:
prompt> sudo apt-get install python2.6-dev # The next line wouldn't install without this.
prompt> sudo easy_install PyXML
-------开始错误-------username@ubuntu:~/data/code$ MyScript.py
-------BEGIN ERROR--------- username@ubuntu:~/data/code$ MyScript.py
Traceback (most recent call last):
File "/home/username/data/code/app/trunk/MyScript.py", line 17, in <module>
from xml import xpath
File "/usr/local/lib/python2.6/dist-packages/PyXML-0.8.4-py2.6-linux-i686.egg/_xmlplus/xpath/__init__.py", line 112, in <module>
from pyxpath import ExprParserFactory
File "/usr/local/lib/python2.6/dist-packages/PyXML-0.8.4-py2.6-linux-i686.egg/_xmlplus/xpath/pyxpath.py", line 59, in <module>
from xml.xpath.ParsedAbbreviatedRelativeLocationPath import ParsedAbbreviatedRelativeLocationPath
File "/usr/local/lib/python2.6/dist-packages/PyXML-0.8.4-py2.6-linux-i686.egg/_xmlplus/xpath/ParsedAbbreviatedRelativeLocationPath.py", line 31
as = ParsedAxisSpecifier.ParsedAxisSpecifier('descendant-or-self')
^
SyntaxError: invalid syntax
-------------结束错误-------------------
-------------END ERROR-------------------
我对 PyXML 的使用已经达到极限.我只想读取一个 xml 文件并使用 xpath 读取/写入数据.是否有一个更简单的库,可以轻松开箱即用?或者关于如何解决这个问题的任何想法?
I'm about at my limits with PyXML. I simply want to read an xml file and read/write data with xpath. Is there a simpler library that will easily work out of the box? Or any ideas on how to fix this?
推荐答案
PyXML 应该是为非常旧的 Python 版本 (< 2.4) 编写的,它使用后来的关键字as"之一作为其变量.如果您的要求很简单,您可以使用 Python 标准库中的 ElementTree它支持 XPath 表达式.一个例子是这里.
PyXML should have been written for very old version of Python (< 2.4) and it used one of the later keywords 'as' as its variable. If your requirement is simple, you can just use ElementTree from Python Standard library which has support for XPath expressions. An example is here.
要使用标准库模块,请执行以下操作:
For using the standard library module, do:
from xml.etree.ElementTree import ElementTree
这篇关于Ubuntu 上的 PyXML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!