XML Declaration standalone=“yes” lxml

前端 未结 5 1223
暗喜
暗喜 2020-12-20 13:01

I have an xml I am parsing, making some changes and saving out to a new file. It has the declaration

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-20 13:13

    etree.tostring(tree, pretty_print = True, xml_declaration = True, encoding='UTF-8')
    

    Will add the declaration if you're using lxml, however I noticed their declaration uses semi-quotes instead of full quotes.

    You can also get the exact declaration you want by just concatenating the output with a static string you need:

    xml = etree.tostring(tree, pretty_print = True, encoding='UTF-8')
    xml = '\n' + xml
    

提交回复
热议问题