我使用以下代码缩进,如上所述here:
parser = etree.XMLParser(remove_blank_text=True)
tree = etree.parse(filename, parser)
但是,原始XML文件缩进了4个空格,使用上面的代码后缩进为2个空格。有什么方法可以保留4个空格缩进?
ADDED
我正在写这样的树:
xml = etree.parse("/path/to/the/xml/file")
with open("/file.xml", "wb") as f:
f.write(etree.tostring(xml, pretty_print=True, encoding='utf8'))
后面是上面的parse
和tree
变量。