Printing each attribute with newline using Element from lxml
Asked Answered
S

1

11

I am trying to print XML out of an Element object, so that formatting allows us to print the tag attributes in new line.

    elem = etree.Element()  //Some element
    str = etree.tostring(elem, pretty_print=True)

The current output looks like

    <module name="A" description="abc" type="xyz">
        <container/>
    </module>

Formatting needed

    <module 
      name="A" 
      description="abc" 
      type="xyz">
      <container/>
    </module>

Is there any existing library that allows us to print newlines for all the attributes present in the tags.

Salto answered 12/4, 2018 at 21:9 Comment(0)
J
1

Etree cannot format attributes like that to my knowledge.

Alternatively, you could try tidylib(http://www.html-tidy.org/) to do the formatting.

On Ubuntu you can do:

sudo apt install tidy
sudo pip install tidylib

Then to format with each attribute on a new line try something like:

>>> from tidylib import tidy_document
>>> k = """<module name="A" description="abc" type="xyz">
        <container/>
    </module>
"""
>>> document, errors = tidy_document(k, options={'indent-attributes':'yes', 'input-xml':'yes'})
>>> print(document)
<module name="A"
        description="abc"
        type="xyz">
  <container />
</module>

Tidy is a vast library with many features and you can read more about the indent-attributes feature here: http://api.html-tidy.org/tidy/quickref_5.6.0.html#indent-attributes

Jumada answered 16/4, 2021 at 19:4 Comment(1)
Close, but the it leaves the first attribute on the same line, and then aligns with the start there. This gives uneven indentations depending on the length of the tag name.Marrowbone

© 2022 - 2024 — McMap. All rights reserved.