亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用python腳本生成xml文件時出現縮進錯誤

使用python腳本生成xml文件時出現縮進錯誤

白衣非少年 2023-08-15 16:58:49
我正在嘗試通過讀取 Excel 工作表使用 python 腳本創建 XML 文件。使用 yattag 我能夠完成此任務,盡管不完全是我需要的格式。我已經粘貼了下面的代碼,并且已經驗證了沒有空格/制表符的混合。目標是將整個項目包裝在“node”標簽中,并為兩個“category”標簽再添加 2 個子類別。我收到錯誤,因為在“節點”標簽之后,“位置”選項卡之前有 2 個選項卡。如果我修復了錯誤,我就會得到第一組代碼?;旧现恍枰獙ⅰ?lt;/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]):        )
查看完整描述

1 回答

?
慕運維8079593

TA貢獻1876條經驗 獲得超5個贊

這應該會給你你正在尋找的 xml:


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)  # invalid


with tag('root'):  # required for valid xml

    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]):

                    with tag("attribute", name='Function'): text(row[7])

                    with tag("attribute", name='Commodity'): text(row[8])

                    with tag("attribute", name='Sub-Commodity'): text(row[9])

                    with tag("attribute", name='Contract/Document Owner'): text(row[10])

                    with tag("subitems"): text("reapply")

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

                    with tag("attribute", name='Supplier'): text(row[12])

                    with tag("attribute", name='Pricing Terms'): text(row[13])

                    with tag("attribute", name='Term Type'): text(row[14])

                    with tag("subitems"): text("reapply")

                


result = indent(

doc.getvalue(),

indentation = '    ',

indent_text = False

)


with open("test_resulted.xml", "w") as f:

   f.write(result)

輸出


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

<root>

    <node type="2" action="2">

        <location>2</location>

        <title>2</title>

        <file>2</file>

        <mime>2</mime>

        <category name="2">

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

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

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

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

            <subitems>reapply</subitems>

        </category>

        <category name="2">

            <attribute name="Supplier">2</attribute>

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

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

            <subitems>reapply</subitems>

        </category>

    </node>

    <node>

       ..........

    </node>

    ..............

</root>


查看完整回答
反對 回復 2023-08-15
  • 1 回答
  • 0 關注
  • 210 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號