updating python-elementtree to overcome xpath selector issue
题
I'm getting an error when searching for the attribute of a node in my xml using ElementTree.
expected path separator ([)
The source code is:
home_team_node = game_node.find( "team/team-metadata[@alignment='home']" )
This discussion on Stackoverflow leads me to conclude I need a newer version of ElementTree.
ElementTree XPath - Select Element based on attribute
When I list installed packages I see I have python-elementtree version 1.2.6-14. Yet even after running apt-get update, any effort to upgrade python-elementtree tells me I've got the latest version. I'm running Ubuntu 9.10, so maybe the repositories don't have elementtree 1.3.
What can I do to upgrade to version 1.3?
解决方案
lxml is a very useful library for parsing XML, and it has a complete implementation of the ElementTree API. Try installing it with:
sudo apt-get install python-lxml
You'll then need to change your import statments to use the lxml version instead of the bundled elementree
.
(I would resist using pip
or easy_install
unless you're in a virtualenv
environment, to avoid confusion with the Debian-packaged Python libraries.)
其他提示
Try installing lxml, ElementTree or cElementTree with either easy_install
or pip
. These packages are likely more recent than the ones in the apt
repositories.
HTH