Issue
I have a xml file for which I have to update a value of tag. Below is the content of the file
<annotation>
<folder>train</folder>
<filename>Arms1.jpg</filename>
<path>D:\PyCharmWorkSpace\Tensorflow\workspace\training_demo\images\train\Arms1.jpg</path>
<source>
<database>Unknown</database>
</source>
</annotation>
In the above content, I have to update the value of path
with new valueBelow is the code I have:
from bs4 import BeautifulSoup
import os
curr_dir = os.path.join(os.path.dirname(__file__), 'img')
files = os.listdir(curr_dir)
for file in files:
if ".xml" in file:
file_path = os.path.join(curr_dir, file)
with open(file_path, 'r') as f:
xml_data = f.read()
bs_data = BeautifulSoup(xml_data, "xml")
bs_data.path.string = "C:\"
xml_file = open(file_path, "w")
xml_file.write(bs_data.prettify())
But its not getting updated in xml file. Can anyone please help. Thanks
Solution
Using ElementTree (no need for any external lib)
import xml.etree.ElementTree as ET
xml = '''<annotation>
<folder>train</folder>
<filename>Arms1.jpg</filename>
<path>D:\PyCharmWorkSpace\Tensorflow\workspace\training_demo\images\train\Arms1.jpg</path>
<source>
<database>Unknown</database>
</source>
</annotation>'''
root = ET.fromstring(xml)
root.find('path').text = 'new path value goes here'
ET.dump(root)
output
<annotation>
<folder>train</folder>
<filename>Arms1.jpg</filename>
<path>new path value goes here</path>
<source>
<database>Unknown</database>
</source>
</annotation>
Answered By - balderman
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.