简体   繁体   English

如何在Python中漂亮地打印xml文件?

[英]How to pretty print an xml file in Python?

I'd like to tidy a complicated xml file, using lxml. 我想使用lxml整理一个复杂的xml文件。 The problem is it has many elements which have tail. 问题在于它有很多带有尾巴的元素。 For example, there's an xml like this: 例如,有一个这样的xml:

 <body><part>n</part> attend </body>

I want to tidy this into this: 我想把它整理成这样:

 <body>
    <part>n</part> attend 
 </body>

I tried to apply pretty_print with remove_blank_text parser in lxml at first. 我最初尝试在lxml中使用带有remove_blank_text解析器的pretty_print。 But it failed. 但是失败了。

import lxml.etree as ET
xml_doc = '<body><part>n</part> attend </body>'
parser = ET.XMLParser(remove_blank_text=True)
root = ET.fromstring(xml_doc, parser)
print(ET.tostring(root, pretty_print=True))
>>>b'<body><part>n</part> attend </body>\n'

And then, I tried again without applying the parser to no avail. 然后,我再次尝试,但没有应用解析器无济于事。

import lxml.etree as ET
xml_doc = '<body><part>n</part> attend </body>'
root = ET.fromstring(xml_doc)
print(ET.tostring(root, pretty_print=True))
>>>b'<body><part>n</part> attend </body>\n'

If the pretty_print attribute does not help, you can probably write your own recursive method to do a pretty print. 如果pretty_print属性没有帮助,则可能可以编写自己的递归方法来进行漂亮的打印。 Something on the lines of 一些东西


def pprint(root, indentTabs = 0):
    print "<%s%s>" % (indentTabs*"\t", root.tag)
    print (indentTabs+1)*"\t" + root.value
    for element in root.children():
        pprint (element, indentTabs+1)
    print "</%s%s>" % (indentTabs*"\t", root.tag)

Though there might be some already available options. 虽然可能已经有一些可用的选项。 The above method would take care of just tags. 上面的方法只处理标签。 You might need to add code to take care of xml attributes as well, if they are present in your xml. 如果xml属性存在于xml中,则可能还需要添加代码来处理xml属性。

EDIT: The above will print in the format 编辑:上面将以格式打印

<tag>
    text
</tag>

You can modify it further according to the output you need. 您可以根据需要的输出进一步对其进行修改。

我遇到了同样的问题,并使用tounicode()为我解决了这个问题。

print(ET.tounicode(root, pretty_print=True))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM