pretty_print in etree.tostring() xml python
我正试图用漂亮的打印选项打印出XML文档。但这是个错误
typeError:ToString()得到意外的关键字参数"pretty_print"
我是不是错过了什么?
1 2 3 4 5 | def CreateXML2(): Date = etree.Element("Date", value=time.strftime(time_format, time.localtime())); UserNode = etree.SubElement(Date,"User"); IDNode = etree.SubElement(UserNode,"ID"); print(etree.tostring(Date, pretty_print=True)); |
问题似乎在于
你看过StackOverflow中的这篇文章吗?我认为它涵盖了你想要的:
就地预打印格式设置工具1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | def indent(elem, level=0): i =" " + level*" " if len(elem): if not elem.text or not elem.text.strip(): elem.text = i +" " if not elem.tail or not elem.tail.strip(): elem.tail = i for elem in elem: indent(elem, level+1) if not elem.tail or not elem.tail.strip(): elem.tail = i else: if level and (not elem.tail or not elem.tail.strip()): elem.tail = i |
示例代码来自post和effbot.org
另外,对于其他信息,您没有正确地调用toString()方法。查看python的网站了解更多信息。