首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用python脚本生成xml文件时出现缩进错误

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

Stack Overflow用户
提问于 2020-09-17 05:11:30
回答 1查看 168关注 0票数 0

我正在尝试通过读取excel表格来创建一个带有python脚本的XML文件。使用yattag我能够做到这一点,尽管不完全是我所需要的格式。我已经粘贴了下面的代码,并且已经验证了没有空格/制表符的混合。

我们的目标是将整个项目包装在“node”标签中,并为两个“category”标签增加两个子类别。我得到了这个错误,因为在'node‘标签之后,在'location’标签之前有两个标签。如果我修复了这个错误,我会得到第一组代码。基本上只需要将“

代码语言:javascript
复制
<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>
        <subitems>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>

代码语言:javascript
复制
     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]):
                    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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-17 06:12:47

这应该会给您提供您正在寻找的xml:

代码语言:javascript
复制
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)

输出

代码语言:javascript
复制
<?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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63927967

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档