我需要在更改后将XML ElementTree转换为String.这是toString部分无法正常工作.
import xml.etree.ElementTree as ET
tree = ET.parse('my_file.xml')
root = tree.getroot()
for e in root.iter('tag_name'):
e.text = "something else" # This works
# Now I want the the complete XML as a String with the alteration
Run Code Online (Sandbox Code Playgroud)
我已经尝试了以下各种版本的版本,ET或ElementTree作为各种名称,并导入toString等等,
s = tree.tostring(ET, encoding='utf8', method='xml')
Run Code Online (Sandbox Code Playgroud)
我已经看到将Python Python ElementTree转换为字符串和其他一些,但我不确定如何将它应用于我的示例.