python - Edit XML file text based on path -


i have xml file (e.g. jerry.xml) contains data given below.

<data> <country name="peru">     <rank updated="yes">2</rank>     <language>english</language>     <currency>1.21$/kg</currency>      <gdppc month="06">141100</gdppc>     <gdpnp month="10">2.304e+0150</gdpnp>     <neighbor name="austria" direction="e"/>     <neighbor name="switzerland" direction="w"/> </country> <country name="singapore">     <rank updated="yes">5</rank>     <language>english</language>     <currency>4.1$/kg</currency>      <gdppc month="05">59900</gdppc>     <gdpnp month="08">1.9e-015</gdpnp>     <neighbor name="malaysia" direction="n"/> </country> 

i extracted full paths of selected texts xml above using code below. reasons given in post.

def extractnumbers(path, node):     nums = []      if 'month' in node.attrib:         if node.attrib['month'] in ['05', '06']:             return nums      path += '/' + node.tag     if 'name' in node.keys():         path += '=' + node.attrib['name']      elif 'year' in node.keys():         path += ' ' + 'month' + '=' + node.attrib['month']     try:         num = float(node.text)         nums.append( (path, num) )     except (valueerror, typeerror):         pass     e in list(node):         nums.extend( extractnumbers(path, e) )     return nums  tree = et.parse('jerry.xml') nums = extractnumbers('', tree.getroot()) print len(nums) print nums 

this gives me location of elements need change shown in colomn 1 of csv below (e.g. hrong.csv).

path                                                      text1       text2       text3       text4       text5  '/data/country name=singapore/gdpnp month=08';            5.2e-015;   2e-05;      8e-06;      9e-04;      0.4e-05;    '/data/country name=peru/gdppc month=06';                 0.04;       0.02;       0.15;       3.24;       0.98;                                                  

i replace text of elements of original xml file (jerry.xml) in column 2 of hrong.csv above, based on location of elements in column 1.

i newbie python , realize might not using best approach. appreciate regards direction wrt this. need parse selected texts nodes of xml file, modify selected text nodes , save each file.

thanks

you should able use xpath capabilities of module this:

import xml.etree.elementtree et tree = et.parse('jerry.xml') root = tree.getroot() data in root.findall(".//country[@name='singapore']/gdpnp[@month='08']"):     data.text = csv_value  tree.write("filename.xml") 

so need rewrite path in csv match xpath rules defined module (see supported xpath rules).


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -