Hi,
I'm new to programming and especially to Python. I have this xml file that has parts like this:
<VECTOR_AVERAGE name="Density Correlations" nvalues="15625">
<SCALAR_AVERAGE indexvalue="( 1,2,0 ) -- ( 3,4,5)">
<COUNT>204160</COUNT>
<MEAN method="jackknife">6.368e-05</MEAN>
<ERROR converged="yes" method="jackknife">2.89e-05</ERROR>
<VARIANCE method="simple">6.37e-05</VARIANCE>
<AUTOCORR method="jackknife">0.843</AUTOCORR>
I need to save MEAN and ERROR. I wrote the following code which works well for me.
import sys
from xml.dom.minidom import parse
# Load XML into tree structure
tree = parse(sys.stdin)
#Find all VECTOR_AVERAGE nodes
va_list = tree.getElementsByTagName('SIMULATION')[0].getElementsByTagName('AVERAGES')[0].getElementsByTagName('VECTOR_AVERAGE')
#Find the 'Density Correlations' node in the list of VECTOR_AVERAGEs
for va in va_list:
if va.attributes.getNamedItem('name').nodeValue == 'Density Correlations':
density_correlations = va
break
#Get a list of all the SCALAR_AVERAGES
sa_list = density_correlations.getElementsByTagName('SCALAR_AVERAGE')
#Initialize lists for holding the data
indexvalue_list = []
mean_list = []
error_list = []
variance_list = []
autocorr_list = []
#Iterate over all SCALAR_AVERAGEs and put their values into the lists
#above.
for sa in sa_list:
indexvalue_list.append(sa.attributes.getNamedItem('indexvalue').nodeValue)
mean_list.append(float(sa.getElementsByTagName('MEAN')[0].childNodes[0].data))
error_list.append(float(sa.getElementsByTagName('ERROR')[0].childNodes[0].data))
variance_list.append(float(sa.getElementsByTagName('VARIANCE')[0].childNodes[0].data))
autocorr_list.append(float(sa.getElementsByTagName('AUTOCORR')[0].childNodes[0].data))
#Output the data in a 5 column format
print "#indexvalue MEAN\tERROR\tVARIANCE\tAUTOCORR"
for i in range(len(mean_list)):
print "%s\t%g\t%g\t%g\t%g" % (indexvalue_list[i],mean_list[i],error_list[i],variance_list[i],autocorr_list[i])
But my question is that how can i get the above parameters for some particular 'indexvalue's not for all of them.
I'm just interested to get the values for indexvalues that look like this:
( 1,4,3 ) -- ( 1,4,3 ) not all of them. ( or in general like this: ( x,y,z ) -- ( x,y,z )
Anyone has any idea to help me with this?
I really appreciate it.