Pretty print in lxml is failing when I add tags to a parsed tree

后端 未结 2 1637
无人及你
无人及你 2020-12-05 17:17

I have an xml file that I\'m using etree from lxml to work with, but when I add tags to it, pretty printing doesn\'t seem to work.

>>> from lxml imp         


        
相关标签:
2条回答
  • 2020-12-05 17:55

    It has to do with how lxml treats whitespace -- see the lxml FAQ for details.

    To fix this, change the loading part of the file to the following:

    parser = etree.XMLParser(remove_blank_text=True)
    root = etree.parse('file.xml', parser).getroot()
    

    I didn't test it, but it should indent your file just fine with this change.

    0 讨论(0)
  • 2020-12-05 18:02

    I was having the same issue when writing to files, for anyone else with this issue:

    I created a helper function that pretty_prints after I run my main function.

    from lxml import etree
    
    def ppxml(xml):
        parser = etree.XMLParser(remove_blank_text=True)
        tree = etree.parse(xml, parser)
        tree.write(xml, encoding='utf-8', pretty_print=True, xml_declaration=True)
    

    In in my main program file

    if __name__ == '__main__':
        main()
        ppxml(xml)
    
    0 讨论(0)
提交回复
热议问题