Setting attribute values of XML using Python's Etree
Question: I am trying to modify XML using Python's Etree Module. I am trying to look
through a fairly complicated XML for a 'ScanItem'. The children of ScanItem
has three keys (['name', 'type', 'value']). I am looking to modify the
contents of 'value'.

Here is my code so far:

    
    
    from lxml import etree
    
    model_dir = 'D:\MPhil\Model_Building\Models\Retinoic_acid\[07]\Model_LIne_10'
    model_name = 'M28.cps'
    model_file=os.path.join(model_dir,model_name) #model_file contains the XML
    
    IA=Identifiability_Analysis() # custom class
    copasiML_str= IA.read_copasiML_as_string(model_file)
    copasiML=etree.fromstring(copasiML_str) # parse XML with etree
    parameters_dict=IA.extract_parameters_to_dict(model_file) #extract some parameters from the XML
    reaction_name_i='v1'
    parameter_name_i='Kcat'
    maxi = float(parameters_dict[reaction_name_i][parameter_name_i])*2 #calculating changes
    mini = float(parameters_dict[reaction_name_i][parameter_name_i])/2 # calculating changes
    
    query = "//*[@name='ScanItem']"  #looks for the element called ScanItem
    for j in copasiML_i.xpath(query):
        children = list(j) #gets the children of ScanItem
        for k in children:
            if k.attrib['name']=='Maximum': #finds the 'bit' that I want to change
                copasiML_i.set(k.attrib['value'],maxi) #an attempt at changing the value to maxi. This does not work
    

This last line gives the following error:

    
    
    ValueError: Invalid attribute name u'0.0102086'  #which is the value of 'maxi'
    

Does anybody know what I'm doing wrong?

Thanks


Answer: So I figured it out in the end. Its a simple assignment with the same syntax
as a normal Python dictionary

