Python - Element Tree is removing the XML declaration

后端 未结 2 1817
夕颜
夕颜 2021-01-11 19:37

I\'m writing some XML with element tree.

I\'m giving the code an empty template file that starts with the XML declaration:

2条回答
  •  时光取名叫无心
    2021-01-11 19:57

    There are different versions of ElementTree. Some of them accept the xml_declaration argument, some do not.

    The one I happen to have does not. It emits the declaration if and only if encoding != 'utf-8'. So, to get the declaration, I call write(filename, encoding='UTF-8').

提交回复
热议问题