首页 > 解决方案 > 为 XML 文件 Python 中的子元素添加索引

问题描述

我是python的新手,所以请帮忙。我想以与元素具有索引相同的方式向row和元素添加索引。columnpage

在第 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>

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

标签: pythonxmlindexing

解决方案


见下文

('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>

推荐阅读