XML : Python XML or txt find and replace

I have an xml file where I want to add some data. I dont want to mess up with the XML tools in python so I modified my input XML file so that I just need to replace some areas in the text. Here is my script.

  f1 = open('import.xml', 'r')  f2 = open('output.xml', 'w')  InvoiceNo = raw_input('InvoiceNo: ')  Name = raw_input('Name: ')  Country = raw_input('Country (Bsp.: US): ')  GrossWeight = raw_input('GrossWeight (Bsp.: 0.7): ')  for line in f1:      f2.replace('<CommercialReferenceNumber></CommercialReferenceNumber>',     '<CommercialReferenceNumber>%s / %s</CommercialReferenceNumber>' % (Name,     InvoiceNo))      f2.write(line.replace('<DestinationCountry></DestinationCountry>', '<DestinationCountry>%s</DestinationCountry>' % (Country)))      f2.write(line.replace('<TotalGrossMassMeasure></TotalGrossMassMeasure>', '<TotalGrossMassMeasure>%s</TotalGrossMassMeasure>' % (GrossWeight)))      f2.write(line.replace('<LocalReferenceNumber></LocalReferenceNumber>', '<LocalReferenceNumber>%s</LocalReferenceNumber>' % (InvoiceNo)))    f1.close()  f2.close()    

The problem is that if I do it like this I get each line 4 times in the output.xml.

I just want to replace these 4 text phrases but there is no way for me.

thanks

No comments:

Post a Comment