[XML-SIG] Learning to use elementtree

Doran, Harold HDoran at air.org
Tue Apr 1 14:35:37 CEST 2008

David et al:

Attached is a sample xml file. Below is my python code. I am using
python 2.5.2 on a Windows XP machine.

from xml.etree.ElementTree import ElementTree as ET

# create a new file defined by the user
f = open('output.txt', 'w')

et = ET(file='g:\python\ml\out_g3r_b2.xml')

for statentityref in
   for statval in
      print >> f, statentityref.attrib['id'], '\t',
statval.attrib['type'], '\t', statval.attrib['value']     

If you run this you will see the output organized almost exactly as I
need it. But, there is a bug in my program, which I suspect is in the
order in which I am looping. For example, here is a snippet of output
from the file output.txt. I've added in some comments so you can see
where I am struggling.

9568 	OmitCount 	0.000000 # This is correct
9568 	NotReachedCount 	0.000000 # This is correct
9568 	PolyserialCorrelation 	0.602525 # This is correct
9568 	AdjustedPolyserial 	0.553564 # This is correct
9568 	AverageScore 	0.817348 # This is correct
9568 	StdevItemScore 	0.386381 # This is correct
9568 	OmitCount 	0.000000 # This is NOT correct
9568 	NotReachedCount 	0.000000 # This is NOT correct
9568 	PolyserialCorrelation 	0.672088 # This is NOT correct
9568 	AdjustedPolyserial 	0.590175 # This is NOT correct
9568 	AverageScore 	1.034195 # This is NOT correct
9568 	StdevItemScore 	0.926668 # This is NOT correct

Now, here is what *should* be returned. Note that I have manually
changed the item id (the number preceding the text) to 9569. The data
are pulled in correctly, but for some reason I am not looping properly
to get the correct item ID to line up with its corresponding data.

9568 	OmitCount 	0.000000
9568 	NotReachedCount 	0.000000
9568 	PolyserialCorrelation 	0.602525
9568 	AdjustedPolyserial 	0.553564
9568 	AverageScore 	0.817348
9568 	StdevItemScore 	0.386381
9569 	OmitCount 	0.000000    # Note the item ID has been modified
here and below.
9569 	NotReachedCount 	0.000000
9569 	PolyserialCorrelation 	0.672088
9569 	AdjustedPolyserial 	0.590175
9569 	AverageScore 	1.034195
9569 	StdevItemScore 	0.926668

Last, notice the portion of code


I know this is what to use only because I manually went through the xml
file to examine its hierarchical structure. I assume this is bad
pratice. Is there a way to examine the parent-child structure of an XML
file in python so I can see the hierarchical structure?


> -----Original Message-----
> From: David Stanek [mailto:dstanek at dstanek.com] 
> Sent: Tuesday, April 01, 2008 6:39 AM
> To: Doran, Harold
> Cc: xml-sig at python.org
> Subject: Re: [XML-SIG] Learning to use elementtree
> 2008/3/31 Doran, Harold <HDoran at air.org>:
> >
> > But, now I am trying to apply the code I have written to a real xml 
> > file I need to work with and things are hitting a road block. Is 
> > anyone on this able willing to look at an xml file I can 
> send them and 
> > work with me through a small example to see if I can get 
> this to work?
> >
> What is the road block?
> --
> David
> http://www.traceback.org
-------------- next part --------------
A non-text attachment was scrubbed...
Name: out_g3r_b2.xml
Type: text/xml
Size: 64655 bytes
Desc: out_g3r_b2.xml
Url : http://mail.python.org/pipermail/xml-sig/attachments/20080401/928e2491/attachment-0001.bin 

More information about the XML-SIG mailing list