첨부 소스 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170
from xml.etree.ElementTree import Element, SubElement, dump

def indent(node, level = 0):
    i = "\n" + level * " " * 4
    if len(node):
        if not node.text or not node.text.strip():
            node.text = i + " " * 4
        if not node.tail or not node.tail.strip():
            node.tail = i
        for node in node:
            indent(node, level + 1)
        if not node.tail or not node.tail.strip():
            node.tail = i
    else:
        if level and (not node.tail or not node.tail.strip()):
            node.tail = i

noteElement = Element("note")

noteElement.attrib["date"] = "20171219"

SubElement(noteElement, "to"     ).text = "Tove"
SubElement(noteElement, "from"   ).text = "Jani"
SubElement(noteElement, "heading").text = "Reminder"
SubElement(noteElement, "body"   ).text = "Don't forget me this weekend!"

indent(noteElement)

dump(noteElement)

"""
<note date="20171219">
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don't forget me this weekend!</body>
</note>
"""
728x90
반응형
그리드형(광고전용)
Posted by icodebroker
TAG ,

댓글을 달아 주세요