更改 lxml 中 etree.tostring 的默认缩进

发布于 2024-07-30 06:06:12 字数 234 浏览 3 评论 0原文

我有一个 XML 文档,我使用 lxml.etree.tostring 对其进行了漂亮的打印。

print etree.tostring(doc, pretty_print=True)

默认的缩进级别是 2 个空格,我想将其更改为 4 个空格。 tostring 函数中没有任何参数; 有没有办法用 lxml 轻松做到这一点?

I have an XML document which I'm pretty-printing using lxml.etree.tostring

print etree.tostring(doc, pretty_print=True)

The default level of indentation is 2 spaces, and I'd like to change this to 4 spaces. There isn't any argument for this in the tostring function; is there a way to do this easily with lxml?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

失而复得 2024-08-06 06:06:12

从版本 4.5 开始,您可以使用 indent() 函数设置缩进大小

etree.indent(root, space="    ")
print(etree.tostring(root))

Since version 4.5, you can set indent size using indent() function.

etree.indent(root, space="    ")
print(etree.tostring(root))
余生共白头 2024-08-06 06:06:12

正如此线程中所述,没有真正的方法可以改变lxml.etree.tostring 漂亮打印的缩进。

但是,您可以:

  • 添加 XSLT 转换来更改缩进
  • 在树中添加空格,类似于 cElementTree

代码:

def indent(elem, level=0):
    i = "\n" + 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

As said in this thread, there is no real way to change the indent of the lxml.etree.tostring pretty-print.

But, you can:

  • add a XSLT transform to change the indent
  • add whitespace to the tree, with something like in the cElementTree library

code:

def indent(elem, level=0):
    i = "\n" + 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
随心而道 2024-08-06 06:06:12

使用 XMLParser 和缩进可以轻松完成此操作。 不需要 Pretty_print :

parser = etree.XMLParser(remove_blank_text=True)
tree = etree.parse('myfile.xml',parser) 
etree.indent(tree, space="    ")
tree.write('myfile.xml', encoding='UTF-8')

This can be easily done, using XMLParser and indent. There is no need for pretty_print :

parser = etree.XMLParser(remove_blank_text=True)
tree = etree.parse('myfile.xml',parser) 
etree.indent(tree, space="    ")
tree.write('myfile.xml', encoding='UTF-8')
十雾 2024-08-06 06:06:12

您可以查看此解决方案。 更改 space 值可以让您获得所需的任何缩进。 它可以是不同数量的空格或制表符 "\t" 字符。

You may check this solution. Changing the space value allows you to get any indent you want. It can be different amount of spaces or tab "\t" character(s).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文