我刚刚完成了Ubuntu 10.10的全新安装,并且试图运行一些使用xml和xpath的脚本。我从PyXML内部收到错误。

我认为这是安装错误。要安装此软件,我执行了以下操作:

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

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


-------------结束错误-------------------

我将尽我所能使用PyXML。我只是想读取一个xml文件并使用xpath读取/写入数据。是否有一个更简单的库可以轻松使用?或关于如何解决此问题的任何想法?

最佳答案

PyXML应该是为非常老的Python版本(ElementTree,该库支持XPath表达式。一个示例是here

要使用标准库模块,请执行以下操作:

from xml.etree.ElementTree import ElementTree

10-06 01:55