Python lxml - creating different forks of tree -


I edit to open an xml file using lxml, XML and then trying to save out different versions I am not, but I can not do it before removing the section to save, but to save the other, leave them in, to save 1 in the right way, to remove the section Is there, but there are still 2 deficiencies in which Area to leave. What am i doing wrong Here is a code example:

  lxml import etree parser = etree.XMLParser (remove_blank_text = true) tree = etree.parse (pathToXmlFile, parser) root = tree.getroot () etree1 = etree Tree1 from = tree Etreelkstrip_elements (Treel, 'product') tree1.write (pathToXmlFileOut1, pretty_print = true, Xml_declaration = true, encoding = 'UTF-8') etree2 = etree tree2 = tree tree2.write (pathToXmlFileOut2, pretty_print = true , xml_declaration = true, encoding = 'UTF-8')  

the problem is that Is tree 1 and tree2 are the same object (pointing to the same object in memory) While) you can not modify a variable without having to modify the other. For example:

  print Treel == Tree2 # true print Treel Tree2 # This may be true  

that you can save for the first time Tree2 save tree1 without modifying it and then with modification. Or maybe use a coop function to create two different objects.


Comments