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 :)

4
  • 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 Commented Oct 22, 2022 at 15:00
  • Try looking at How to search and replace text in an XML file using Python? Commented Oct 22, 2022 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 Commented Oct 22, 2022 at 15:07
  • Thanks @OldBill I just used that instead since I know for certain all the 'Rom1' should be replaced Commented Oct 22, 2022 at 15:14

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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))

Comments

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

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.