向 XML 文件 Python 中的子元素添加索引

格案例

我是python的新手,所以请帮忙。我想以元素具有索引相同的方式rowcolumn元素添加page索引。

在第 1 页有 4 行,所以索引将从 0 到 3。在第 1 页,第 0 行只有一列,所以索引将只有 0。在第 1 页,第 2 行有 3 列,所以索引对于列将从 0 到 2。其他页面中的其他行也是如此。

我已经开始使用 Elementree 进行测试,但只是打印元素的基础知识。也许有人可以帮我解决这个问题。

我只有以下代码用于基本测试,但我不知道如何进行。

import xml.etree.ElementTree as ET
tree = ET.parse('smp.xml')
root = tree.getroot()

for text in root.iter('text'):
    print(text.attrib)

for text in root.iter('text'):
    print(text.text)

输入的 XML 是这样的:

<?xml version="1.0"?>
<doc>
    <page index="0"/>
    <page index="1">
        <row>
            <column>
                <text>fibrous drupe</text>
            </column>
        </row>
        <row>
            <column>
                <text>follicle</text>
            </column>
            <column>
                <text>legume</text>
            </column>
        </row>
        <row>
            <column>
                <text>loment</text>
            </column>
            <column>
                <text>nut</text>
            </column>
            <column>
                <text>samara</text>
            </column>
        </row>
        <row>
            <column>
                <text>schizocarp</text>
            </column>
        </row>
    </page>
    <page index="2">
        <row>
            <column>
                <text>cypsela</text>
            </column>
        </row>
    </page>
    <page index="3"/>
</doc>

我想将其转换为:

<?xml version="1.0"?>
<doc>
    <page index="0"/>
    <page index="1">
        <row index="0">
            <column index="0">
                <text>fibrous drupe</text>
            </column>
        </row>
        <row index="1">
            <column index="0">
                <text>follicle</text>
            </column>
            <column index="1">
                <text>legume</text>
            </column>
        </row>
        <row index="2">
            <column index="0">
                <text>loment</text>
            </column>
            <column index="1">
                <text>nut</text>
            </column>
            <column index="2">
                <text>samara</text>
            </column>
        </row>
        <row index="3">
            <column index="0">
                <text>schizocarp</text>
            </column>
        </row>
    </page>
    <page index="2">
        <row index="0">
            <column index="0">
                <text>cypsela</text>
            </column>
        </row>
    </page>
    <page index="3"/>
</doc>

我希望有意义。提前致谢。

巴尔德曼

见下文

('56403870.xml' 是您发布的 XML)

import xml.etree.ElementTree as ET

tree = ET.parse('56403870.xml')
root = tree.getroot()

pages = root.findall('.//page')
for page in pages:
    rows = page.findall('.//row')
    for r, row in enumerate(rows):
        row.attrib['index'] = str(r)
        columns = row.findall('.//column')
        for c, col in enumerate(columns):
            col.attrib['index'] = str(c)

ET.dump(tree)

输出

<doc>
    <page index="0" />
    <page index="1">
        <row index="0">
            <column index="0">
                <text>fibrous drupe</text>
            </column>
        </row>
        <row index="1">
            <column index="0">
                <text>follicle</text>
            </column>
            <column index="1">
                <text>legume</text>
            </column>
        </row>
        <row index="2">
            <column index="0">
                <text>loment</text>
            </column>
            <column index="1">
                <text>nut</text>
            </column>
            <column index="2">
                <text>samara</text>
            </column>
        </row>
        <row index="3">
            <column index="0">
                <text>schizocarp</text>
            </column>
        </row>
    </page>
    <page index="2">
        <row index="0">
            <column index="0">
                <text>cypsela</text>
            </column>
        </row>
    </page>
    <page index="3" />
</doc>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章