python - Count occurrences in several xml-files -


is there way count occurrences of identity of type on several xml-files? here's example mean. xml-files have following structure:

<data>   <geo>     <g type="mount" stid="s727" level="geo"/>     <g type="bike" stid="g12" level="geo"/>     <g type="lake" stid="g12432" level="geo"/>     <g type="mount" stid="s0" level="geo"/>   </geo> </data> 

what i'd count number of occurences of "s0", "s"+any number, "s12" per type , same 3 categories g's. can show me how work? of course, last have example on category - important me how save intermediate results , how main-function (i suppose you'll sys.arg - @ least, do...)

thanks help.

i use lxml library type of thing. it's documented. here little script wrote kind of think want. if care count , not items, increment value instead of appending list. also, gets attributes contain s, can modified values begin s if going for.

from lxml import etree stringio import stringio  parser = etree.xmlparser() fileinstance = open('help1.xml', 'r') tree = etree.parse(stringio(fileinstance.read()), parser) fileinstance.close()  output = []  in tree.xpath('//geo'):     element in i:         if "s" in element.attrib['stid']:             output.append(element.attrib['stid'])  print output print len(output) 

Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -