0

I am trying to search and replace certain words in my .xml file and replace it with another, but I struggle a bit.

I have been using this code so far:

import xml.etree.ElementTree as ET

with open('Rom1.xml', encoding="utf8") as f:
  tree = ET.parse(f)
  #root = tree.find('ExportedObjects')
  root = tree.getroot()

  for elem in root.iter():
    try:
      elem.text = elem.text.replace('Rom1', 'Rom2')
  except AttributeError:
    pass

Rom1.xml this is a snapshot from the XML file showing the structure

The XML file is pretty big but it contains the string 'Rom1' 41 times and I would like to replace all of them.

I know a simple search and replace in text editor does the job, but I want to automate this since I will do it for several hundered of files.

Any help is appriciated :)

  • 2
    Providing you know (with absolute certainty) that the replacements will not be ambiguous then you could just read the entire file as text and do a str.replace(). Otherwise you should probably look for specific tags and attributes where you know the value to be replaced might exist – DarkKnight Oct 22 '22 at 15:00
  • Try looking at [How to search and replace text in an XML file using Python?](https://stackoverflow.com/questions/37868881/how-to-search-and-replace-text-in-an-xml-file-using-python?adlt=strict&toWww=1&redig=849075E8A90C4881934DB8A306A7E00D) – Alias Cartellano Oct 22 '22 at 15:03
  • I allready looked at that one, it just makes a copy of my file, it doesn't replace any words. Some of the functions used in that solution doesn't work in python 3.10 anymore, so not 100% sure what functions I should use instead – Asgeir Bakke Oct 22 '22 at 15:07
  • Thanks @OldBill I just used that instead since I know for certain all the 'Rom1' should be replaced – Asgeir Bakke Oct 22 '22 at 15:14

3 Answers3

0

If there is no possibility of ambiguity then you could just do this:

with open('Rom1.xml', encoding='utf-8', mode='r+') as xml:
  content = xml.read().replace('Rom1', 'Rom2')
  xml.seek(0)
  xml.write(content)
  xml.truncate()

In this case the truncate() call is not necessary. However, if the second argument to replace() was shorter than the first then this would be crucial. Just leave it there to account for all eventualities

DarkKnight
  • 19,739
  • 3
  • 6
  • 22
0

Ok so I tried something else with great success:

import xml.etree.ElementTree as ET
Rom2 = input('Number: ')

input_file = "Rom1.xml"
output_file = Rom2+".xml"
with open(input_file) as f:
  xml_content = f.readlines()

with open(output_file, 'w+') as f:
  for line in xml_content:
    f.write(line.replace('Rom1', Rom2))

But if I want to replace a second string f.ex 'SQ4XXX' to 'SQ4050' then it replaces both and keeps the old as well? I'm confused.

import xml.etree.ElementTree as ET
Rom2 = input('Number: ')
sq = input('SQ: ')

input_file = "Rom1.xml"
output_file = Rom2+".xml"
with open(input_file) as f:
  xml_content = f.readlines()

with open(output_file, 'w+') as f:
  for line in xml_content:
    f.write(line.replace('Rom1', Rom2))
    f.write(line.replace('SQ4XXX', sq))
0

Ok I got it working like I wanted, thanks for the help guys!

Heres the final code:

import xml.etree.ElementTree as ET
Rom2 = input('Number: ')
sq4 = input('SQ4: ')
sq5 = input('SQ5: ')

input_file = "Rom1.xml"
output_file = Rom2+".xml"
with open(input_file) as f:
    xml_content = f.readlines()

with open(output_file, 'w+') as f:
    for line in xml_content:
      f.write(line.replace('Rom1', Rom2))

with open(output_file, encoding='utf-8', mode='r+') as xml:
    content = xml.read().replace('SQ4XXX', sq4)
    xml.seek(0)
    xml.write(content)
    xml.truncate()

with open(output_file, encoding='utf-8', mode='r+') as xml:
    content = xml.read().replace('SQ5XXX', sq5)
    xml.seek(0)
    xml.write(content)
    xml.truncate()er code here
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 25 '22 at 07:55