module error for elementtree

S

saif.shakeel

#!/usr/bin/env python


from elementtree import ElementTree as Element
tree = et.parse("testxml.xml")


for t in tree.getiterator("SERVICEPARAMETER"):
if t.get("Semantics") == "localId":
t.set("Semantics", "dataPackageID")


tree.write("output.xml")


Hi,
For the above code to work elementtree is
imported in first line ,but when running it says :
ImportError: No module named elementtree.ElementTree
Does thie module exists as default or a patch is needed?
Thanks
 
H

half.italian

#!/usr/bin/env python

from elementtree import ElementTree as Element
tree = et.parse("testxml.xml")

for t in tree.getiterator("SERVICEPARAMETER"):
if t.get("Semantics") == "localId":
t.set("Semantics", "dataPackageID")

tree.write("output.xml")

Hi,
For the above code to work elementtree is
imported in first line ,but when running it says :
ImportError: No module named elementtree.ElementTree
Does thie module exists as default or a patch is needed?
Thanks

http://groups.google.com/group/comp...fb99/a4523a6e9b7061af?rnum=1#a4523a6e9b7061af

Read carefully.
 
S

Stefan Behnel

The commands are given in that link are more so for a linux system .I
am developing in windows.how should i go about to make it work

As said: read carefully.

Stefan
 
J

Jerry Hill

For the above code to work elementtree is
imported in first line ,but when running it says :
ImportError: No module named elementtree.ElementTree
Does thie module exists as default or a patch is needed?

That depends on which version of Python you are running. Starting
with 2.5, ElementTree was moved into the standard library, as part of
the xml package. If you're using an older version of python, I think
you'll need to download and install the elementtree package from
http://effbot.org/zone/element-index.htm

Assuming you're using python 2.5, you probably want something like this:

from xml.etree.ElementTree import ElementTree as et
 
J

John Machin

That depends on which version of Python you are running. Starting
with 2.5, ElementTree was moved into the standard library, as part of
the xml package. If you're using an older version of python, I think
you'll need to download and install the elementtree package fromhttp://effbot.org/zone/element-index.htm

Assuming you're using python 2.5, you probably want something like this:

from xml.etree.ElementTree import ElementTree as et

1. The OP appears to be confusing ElementTree and Element (objects of
the first class are containers of objects of the second class).

2. For any serious work to be done efficiently, the cElementTree
module should be used.

3. Here's some code that appears to work for supporting multiple
versions of Python:

8< --- import_et.py ---
import sys
python_version = sys.version_info[:2]
print >> sys.stderr, "Python version:", sys.version_info
if python_version >= (2, 5):
import xml.etree.cElementTree as ET
else:
try:
import cElementTree as ET
except ImportError:
try:
import ElementTree as ET
except ImportError:
msg = "\nYou need the [c]ElementTree package\n" \
"from http://effbot.org/downloads/\n\n"
sys.stderr.write(msg)
raise
print >> sys.stderr, "ET imported from", ET.__file__
8< --- end of import_et.py ---

4. and some (occasionally astonishing) results:

C:\junk>for %v in (5,1,4) do \python2%v\python import_et.py

C:\junk>\python25\python import_et.py
Python version: (2, 5, 1, 'final', 0)
ET imported from C:\python25\lib\xml\etree\cElementTree.pyc

C:\junk>\python21\python import_et.py
Python version: (2, 1, 3, 'final', 0)
ET imported from C:\python21\cElementTree.pyd

C:\junk>\python24\python import_et.py
Python version: (2, 4, 3, 'final', 0)
ET imported from C:\Documents and Settings\sjm\Application Data\Python-
Eggs\celementtree-1.0.5_20051216-py2.4-win32.egg-tmp\cElementTree.pyd

IIRC, I have TurboGears (a leading contender for the "All of your
sys.path are belong to us" award) to thank for that little gem :)

HTH,
John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top