Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need some help with writing a python script with a cross file find and replace

ID: 3811956 • Letter: I

Question

I need some help with writing a python script with a cross file find and replace function dealing with XML documents. I have 4 documents that the teacher has given me. How would I write a script prompting the user for the text to find and the text to replace it with and then act on all files in a Data directory at the same level as the script. You could create simple XML documents as an example or if need be I will attach them.

This is the set up I have:

from lxml import etree

xml1Doc = etree.parse('c:Barkeep.xml')
xml2Doc = etree.parse('c:City_Guard.xml')
xml3Doc = etree.parse('c:Farmer.xml')
xml4Doc = etree.parse('c:Traveling_Salesmen.xml')

Explanation / Answer

XML format I have used::

<?xml version='1.0' encoding='utf8'?>
<data>
<items>
<item name="item1">Item</item>
<item name="item2" />
<item name="item1">Item</item>
<item name="item4" />
</items>
</data>