I've written some code to:
- Parse an XML file.
- Extract some strings.
- Make copies of the original XML whose names are suffixed with the extracted strings.
Please review this.
from lxml import etree as ET
import re
def create_paramsuffix_copies(xmlfile):
NSMAP = {"c": "http://www.copasi.org/static/schema"}
parsed = ET.parse(xmlfile)
list_of_params = []
for a in parsed.xpath("//c:Reaction", namespaces=NSMAP):
for b in a.xpath(".//c:Constant", namespaces=NSMAP):
list_of_params.append((a.attrib['name'])+'_'+(b.attrib['name']))
return list_of_params
def add_suffix(xmlfile, name_suffix):
parsed = ET.parse(xmlfile) #parse xml
xmlfile = re.sub('.cps', '', xmlfile) #remove suffix
parsed.write(xmlfile+'_'+name_suffix+'.cps',
xml_declaration = True,
encoding = 'utf-8',
method = 'xml')
modelfile = 'ct.cps'
for a in create_paramsuffix_copies(modelfile):
add_suffix(modelfile,a)