我有一個Debian 6安裝程序,我不會觸及太多。lxml問題
$ python --version
Python 2.6.6
$ dpkg --list|grep lxml
ii python-lxml 2.2.8-2
我試着用XPath的訪問是這樣的:
import xml.etree.ElementTree as ET
root = ET.parse("a.xml")
for e in root.findall('.//rec/data/field[@name="id"]'):
print(e.tag + " " + e.attrib.get('name'))
我得到一個錯誤
Traceback (most recent call last):
File "a.py", line 4, in <module>
for e in root.findall('.//rec/data/field[@name="id"]'):
File "/usr/lib/python2.6/xml/etree/ElementTree.py", line 647, in findall
return self._root.findall(path)
File "/usr/lib/python2.6/xml/etree/ElementTree.py", line 355, in findall
return ElementPath.findall(self, path)
File "/usr/lib/python2.6/xml/etree/ElementPath.py", line 198, in findall
return _compile(path).findall(element)
File "/usr/lib/python2.6/xml/etree/ElementPath.py", line 176, in _compile
p = Path(path)
File "/usr/lib/python2.6/xml/etree/ElementPath.py", line 93, in __init__
"expected path separator (%s)" % (op or tag)
SyntaxError: expected path separator ([)
這是XPath語法不Python-2.2.8 lxml的支持?
什麼版本支持這個,並與Python 2.6兼容?
您的標題是關於lxml,但您的代碼使用xml。你也可以在Python 2.7中編寫代碼不知道它是否僅僅是Python 2.6的問題。 –
確認。 Python 2.6中的ElementTree不支持搜索屬性[@attribute]語法。請參閱http://stackoverflow.com/questions/15753714/elementtree-syntaxerror-expected-path-separator –