반응형
from xml.etree.ElementTree import Element, tostring
document = Element('outer')
node = SubElement(document, 'inner')
node.NewValue = 1
print tostring(document) # Outputs "<outer><inner /></outer>"
다음 XML 선언을 포함하려면 문자열이 필요합니다.
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
그러나이를 수행하는 문서화 된 방법이없는 것 같습니다.
ElementTree
에서 XML 선언을 렌더링하는 적절한 방법이 있습니까?
해결 방법
ElementTree.tostring ()
에 방법이없는 것 같다는 사실에 놀랐습니다. 그러나 ElementTree.ElementTree.write ()
를 사용하여 XML 문서를 가짜 파일에 쓸 수 있습니다.
from io import BytesIO
from xml.etree import ElementTree as ET
document = ET.Element('outer')
node = ET.SubElement(document, 'inner')
et = ET.ElementTree(document)
f = BytesIO()
et.write(f, encoding='utf-8', xml_declaration=True)
print(f.getvalue()) # your XML file, encoded as UTF-8
참조 페이지 https://stackoverflow.com/questions/15356641
반응형
'파이썬' 카테고리의 다른 글
파이썬 모델에서 delete ()를 재정의하고 관련 삭제와 함께 작동하도록하려면 어떻게해야합니까? (0) | 2021.01.24 |
---|---|
파이썬 목록의 열을 어떻게 합산 할 수 있습니까? (0) | 2021.01.24 |
파이썬 How to get the first column of a pandas DataFrame as a Series? (0) | 2021.01.24 |
파이썬 Java Equivalent to Python Dictionaries (0) | 2021.01.23 |
파이썬 How can I check if code is executed in the IPython notebook? (0) | 2021.01.23 |
댓글