使用python脚本生成xml文件时出现缩进错误

我正在尝试通过读取 Excel 工作表使用 python 脚本创建 XML 文件。使用 yattag 我能够完成此任务,尽管不完全是我需要的格式。我已经粘贴了下面的代码,并且已经验证了没有空格/制表符的混合。


目标是将整个项目包装在“node”标签中,并为两个“category”标签再添加 2 个子类别。我收到错误,因为在“节点”标签之后,“位置”选项卡之前有 2 个选项卡。如果我修复了错误,我就会得到第一组代码。基本上只需要将“</node”拉到底部(如果有任何意义的话)。


<node type="document" action="create">

        <location>TempCD</location>

        <title>doc1</title>

        <file>E:\Doc1.docx</file>

        <mime>application</mime>

    </node>

    <category name="Content">

        <attribute name="Function">asd</attribute>

        <attribute name="Commodity">sf</attribute>

        <attribute name="Sub-Commodity">qw</attribute>

        <attribute name="Contract/Document Owner">e</attribute>

        <subites>reapply</subitems>

    </category>

    <category name="Content Server Categories:LYB:LYB-GSC-Contracts">

        <attribute name="Supplier">Altom Transport</attribute>

        <attribute name="Pricing Terms">Fixed</attribute>

        <attribute name="Term Type">Fixed</attribute>

        <subitems name="Commodity">reapply</subitems>

    </category>

     from openpyxl import load_workbook

        from yattag import Doc, indent

        

        wb = load_workbook("input_sample.xlsx")

        ws = wb.worksheets[0]

        

        # Create Yattag doc, tag and text objects

        doc, tag, text = Doc().tagtext()

        

        xml_header = '<?xml version="1.0" encoding="UTF-8"?>'

        xml_schema = '<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"></xs:schema>'

        

        doc.asis(xml_header)

        doc.asis(xml_schema)

        

        for row in ws.iter_rows(min_row=2):

            row = [cell.value for cell in row]

            with tag('node', type=row[0], action=row[1]):

                    with tag("location"): text(row[2])

                    with tag("title"): text(row[3])

                    with tag("file"): text(row[4])

                    with tag("mime"): text(row[5])

                with tag('category', name=row[6]):

        )



白衣非少年
浏览 155回答 1
1回答

慕运维8079593

这应该会给你你正在寻找的 xml:from openpyxl import load_workbookfrom yattag import Doc, indentwb = load_workbook("input_sample.xlsx")ws = wb.worksheets[0]# Create Yattag doc, tag and text objectsdoc, tag, text = Doc().tagtext()xml_header = '<?xml version="1.0" encoding="UTF-8"?>'xml_schema = '<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"></xs:schema>'doc.asis(xml_header)#doc.asis(xml_schema)&nbsp; # invalidwith tag('root'):&nbsp; # required for valid xml&nbsp; &nbsp; for row in ws.iter_rows(min_row=2):&nbsp; &nbsp; &nbsp; &nbsp; row = [cell.value for cell in row]&nbsp; &nbsp; &nbsp; &nbsp; with tag('node', type=row[0], action=row[1]):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; with tag("location"): text(row[2])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; with tag("title"): text(row[3])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; with tag("file"): text(row[4])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; with tag("mime"): text(row[5])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; with tag('category', name=row[6]):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; with tag("attribute", name='Function'): text(row[7])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; with tag("attribute", name='Commodity'): text(row[8])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; with tag("attribute", name='Sub-Commodity'): text(row[9])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; with tag("attribute", name='Contract/Document Owner'): text(row[10])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; with tag("subitems"): text("reapply")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; with tag('category', name=row[11]):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; with tag("attribute", name='Supplier'): text(row[12])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; with tag("attribute", name='Pricing Terms'): text(row[13])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; with tag("attribute", name='Term Type'): text(row[14])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; with tag("subitems"): text("reapply")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;result = indent(doc.getvalue(),indentation = '&nbsp; &nbsp; ',indent_text = False)with open("test_resulted.xml", "w") as f:&nbsp; &nbsp;f.write(result)输出<?xml version="1.0" encoding="UTF-8"?><root>&nbsp; &nbsp; <node type="2" action="2">&nbsp; &nbsp; &nbsp; &nbsp; <location>2</location>&nbsp; &nbsp; &nbsp; &nbsp; <title>2</title>&nbsp; &nbsp; &nbsp; &nbsp; <file>2</file>&nbsp; &nbsp; &nbsp; &nbsp; <mime>2</mime>&nbsp; &nbsp; &nbsp; &nbsp; <category name="2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <attribute name="Function">2</attribute>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <attribute name="Commodity">2</attribute>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <attribute name="Sub-Commodity">2</attribute>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <attribute name="Contract/Document Owner">2</attribute>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <subitems>reapply</subitems>&nbsp; &nbsp; &nbsp; &nbsp; </category>&nbsp; &nbsp; &nbsp; &nbsp; <category name="2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <attribute name="Supplier">2</attribute>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <attribute name="Pricing Terms">2</attribute>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <attribute name="Term Type">2</attribute>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <subitems>reapply</subitems>&nbsp; &nbsp; &nbsp; &nbsp; </category>&nbsp; &nbsp; </node>&nbsp; &nbsp; <node>&nbsp; &nbsp; &nbsp; &nbsp;..........&nbsp; &nbsp; </node>&nbsp; &nbsp; ..............</root>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python