Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python脚本生成xml文件时出现缩进错误_Python_Xml_Indentation_Yattag - Fatal编程技术网

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

使用python脚本生成xml文件时出现缩进错误,python,xml,indentation,yattag,Python,Xml,Indentation,Yattag,我试图通过读取excel工作表,用python脚本创建一个XML文件。使用yattag,我能够完成这一点,尽管我不太需要格式化。 我已经粘贴了下面的代码,并且已经验证了没有空格/制表符的混合 目标是将整个项目包装在“节点”标记中,并为两个“类别”标记增加两个子类别。我得到这个错误是因为在'node'标记之后,在'location'选项卡之前有两个选项卡。如果我修复了错误,我会得到第一组代码。基本上只需下拉“”,这将为您提供所需的xml: from openpyxl import load_wor

我试图通过读取excel工作表,用python脚本创建一个XML文件。使用yattag,我能够完成这一点,尽管我不太需要格式化。 我已经粘贴了下面的代码,并且已经验证了没有空格/制表符的混合


目标是将整个项目包装在“节点”标记中,并为两个“类别”标记增加两个子类别。我得到这个错误是因为在'node'标记之后,在'location'选项卡之前有两个选项卡。如果我修复了错误,我会得到第一组代码。基本上只需下拉“”,这将为您提供所需的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)
从openpyxl导入加载\u工作簿
从标签导入单据,缩进
wb=加载工作簿(“input\u sample.xlsx”)
ws=wb.工作表[0]
#创建标签文档、标签和文本对象
doc,tag,text=doc().tagtext()
xml_头=“”
xml_模式=“”
doc.asis(xml_头)
#doc.asis(xml#U模式)#无效
带标记('root'):#对于有效的xml是必需的
对于ws.iter\u行中的行(最小行=2):
行=[行中单元格的cell.value]
带标记('node',type=row[0],action=row[1]):
带标记(“位置”):文本(第[2]行)
带标记(“标题”):文本(第[3]行)
带标记(“文件”):文本(第[4]行)
带标记(“mime”):文本(第[5]行)
带标记('category',name=行[6]):
带标记(“属性”,name='Function'):文本(第[7]行)
带标记(“属性”,name='Commodity'):文本(第[8]行)
带标签(“属性”,name='Sub-Commodity'):文本(第[9]行)
带标记(“属性”,name='Contract/Document Owner'):文本(第[10]行)
带标记(“子项”):文本(“重新应用”)
带标记('category',name=行[11]):
带标签(“属性”,名称='Supplier'):文本(第[12]行)
带标记(“属性”,name='Pricing Terms'):文本(第[13]行)
带标记(“属性”,name='Term Type'):文本(第[14]行)
带标记(“子项”):文本(“重新应用”)
结果=缩进(
doc.getvalue(),
缩进=“”,
缩进文本=假
)
以open(“test_resulted.xml”、“w”)作为f:
f、 写入(结果)
输出

<?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>

2.
2.
2.
2.
2.
2.
2.
2.
重新申请
2.
2.
2.
重新申请
..........
..............

源代码有问题(第19行)。打开带有标记('node')的
块后,不应使用两级缩进:
块。用下面的语句删除四个
语句中每一个的一个缩进级别。我知道有两个缩进级别,但对解决方案考虑过度了。这成功了,谢谢!我很感激,我想得太多了。只需将文档细节的缩进量与工作类别的缩进量相同即可。
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>