ElementTree 写入 XML
ElementTree Write to an XML
将已编辑的 XML 根目录写入新文件的最简单方法是什么?这是我到目前为止所拥有的,它正在抛出 AttributeError: 'module' object has no attribute 'write'
PS:除了 ElementTree,我不能使用任何其他 api。
import xml.etree.ElementTree as ET
from xml.etree.ElementTree import Element, SubElement, Comment
from ElementTree_pretty import prettify
tree = ET.parse('file-to-be-edited.xml')
root = tree.getroot()
#Process XML here
ET.write('file-after-edits.xml')
AttributeError: 'module' object has no attribute 'write' 是说不能直接从 ElementTree class 调用 write 方法,它不是静态方法,请尝试使用 tree.write('file-after-edits.xml')
,树是 ElementTree 中的对象。
您的 tree
是一个 ElementTree
对象,它提供 write()
方法来编写树。例如:
#Process XML here
tree.write('file-after-edits.xml', encoding='utf8')
将已编辑的 XML 根目录写入新文件的最简单方法是什么?这是我到目前为止所拥有的,它正在抛出 AttributeError: 'module' object has no attribute 'write'
PS:除了 ElementTree,我不能使用任何其他 api。
import xml.etree.ElementTree as ET
from xml.etree.ElementTree import Element, SubElement, Comment
from ElementTree_pretty import prettify
tree = ET.parse('file-to-be-edited.xml')
root = tree.getroot()
#Process XML here
ET.write('file-after-edits.xml')
AttributeError: 'module' object has no attribute 'write' 是说不能直接从 ElementTree class 调用 write 方法,它不是静态方法,请尝试使用 tree.write('file-after-edits.xml')
,树是 ElementTree 中的对象。
您的 tree
是一个 ElementTree
对象,它提供 write()
方法来编写树。例如:
#Process XML here
tree.write('file-after-edits.xml', encoding='utf8')