i am trying to update a number of XML files in a specific directory.
I want to update the data between the tags for <author> Smith, Paul </author> and <price> 79.99 </price>.
this needs to be applied for all the files within the same directory.
My code in python does make the update but inserts a new data of author and price, it should over write this data.
Any help will be much appreciated, see below xml and python coding.
See below is one of many XML files i am trying to update update:
<?xml version="1.0"?>
<catalog>
<book id="bk103">
<author>Corets, Eva</author>
<title>Maeve Ascendant</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-11-17</publish_date>
<description>After the collapse of a nanotechnology
society in England, the young survivors lay the
foundation for a new society.</description>
</book>
</catalog>
Current Python code, trying to loop through all files in a specific directory:
import os
import re
my_dir = r'C:\Users\MyDocuments\Desktop\XML Test data1'
replace_what_author = '(?<=<author>)((((.)*,)*)(.)*)(?=</)'
replace_with_author = 'Bloggs, Joe'
replace_what_price = '(?<=<price>)(\d+\.\d\d(?!\d))(?=</)'
replace_with_price = '10.99'
for fn in os.listdir(my_dir):
#print(fn)
pathfn = os.path.join(my_dir,fn)
if os.path.isfile(pathfn):
file = open(pathfn, 'r+')
new_file_content=''
for line in file:
auth = re.compile(replace_what_author)
pri = re.compile(replace_what_price)
new_file_content += auth.sub(replace_with_author,line)
new_file_content += pri.sub(replace_with_price,line)
file.seek(0)
file.truncate()
file.write(new_file_content)
file.close()
xmlmodule and not regex for a better solution