【发布时间】:2021-11-07 14:16:10
【问题描述】:
我有一个 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>
在上面的内容中,我必须用新值更新path的值下面是我的代码:
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())
但它没有在 xml 文件中更新。任何人都可以请帮忙。谢谢
【问题讨论】:
标签: python xml beautifulsoup